property question

Mike C. Fletcher mcfletch at rogers.com
Thu May 23 18:04:24 EDT 2002


 From basicproperty.basic:

     def _getValue( self, client ):
         """Perform a low-level retrieval of the "raw" value for the client

         The default implementation uses the property's name as a key
         into the client's __dict__ attribute.  Note that this will
         fail with objects using __slots__.
         """
         return client.__dict__[ self.name ]
     def _setValue( self, client, value ):
         """Perform a low-level set of the "raw" value for the client

         The default implementation sets the value using the property's
         name as a key in the client's __dict__ attribute.  Note that this
         will fail with objects using __slots__.
         """
         # if the client has a __setattr__, it isn't getting called here
         # should we defer to it by default? I don't know...
         client.__dict__[ self.name ] = value
     def _delValue( self, client ):
         """Perform a low-level delete of the value for the client

         The default implementation deletes the property's name
         from the client's __dict__ attribute.  Note that this will
         fail with objects using __slots__.
         """
         del client.__dict__[ self.name ]

i.e. you can use the client's dictionary object for set/get 
functionality (save if there's a slots declaration, then I don't know 
how to do it).

<shameless plug>
basicproperty, part of the wxPython Properties Distribution, defines a 
set of utility property types.  It's probably one of the most extensive 
Python 2.2. property-related code collections out there, so a good place 
to start if you're looking for sample code for real-world problems.

http://wxpypropdist.sf.net/
</shameless>

HTH,
Mike

Markus Jais wrote:
> hello
> 
> Just started playing with python 2.2 and the new features.
> Now I have a question on properties:
> 
> class Address(object):
...
>         def set_email(self, email):
>                 print "in set email"
>                 self.email = email
...
> I get an endless recustion because I refer to self.email in the get_ and 
> set_ method
> 
> how can I set the value of self.email and how can I return a modified 
> version in the get method??
...






More information about the Python-list mailing list