Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
how to create nested classes dynamically
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Gabriel Genellina  
View profile  
 More options Nov 4, 4:55 pm
Newsgroups: comp.lang.python
From: "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
Date: Wed, 04 Nov 2009 02:55:00 -0300
Subject: Re: how to create nested classes dynamically
En Wed, 04 Nov 2009 02:04:11 -0300, gopal mishra <gop...@infotechsw.com>  
escribió:

First, I assume getBB1 and setBB1 really do some actual work. If they're  
just accessors for an instance attribute, don't use them; don't write Java  
in Python [1]

In Python, you may have instance (or "normal") attributes, and class  
attributes. When you say obj.name, if name is not found as an instance  
attribute, it is looked up on its class (and all its base classes; this is  
how methods are searched, BTW)
Class attributes are "shared" among all instances - that is, all instances  
retrieve the same object when you access a class attribute.

Your code above, as it is written, creates two class attributes named bb  
and cc (they're class attributes because the statements bb=... and cc=...  
are inside the class definition). It works, but perhaps that's not what  
you want. If you want instance attributes instead, create them in  
AA.__init__ by using self.bb=something

Nested classes provide no benefit here and are harder to use, so just move  
the class definitions to the module level. The code would become:

class BB(object):
   def setBB1(self, value):
     ##some code

   def getBB1(self):
     "docstring for the property"
     bb1 = #somecode
     return bb1

   bb1 = property(getBB1, setBB1)
   bb2 = ...

class CC(object):
   ....

class AA(object):
   def __init__(self, ...):
     self.bb = BB()
     self.cc = CC()

Then, you can write:

aa = AA()
print aa.bb.bb1
aa.bb.bb2 = '10'
print aa.bb.bb2

[1] http://dirtsimple.org/2004/12/python-is-not-java.html

--
Gabriel Genellina


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google