Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

property question

3 views
Skip to first unread message

Markus Jais

unread,
May 23, 2002, 5:50:29 PM5/23/02
to
hello

Just started playing with python 2.2 and the new features.
Now I have a question on properties:

class Address(object):

def __init__(self):
self.firstname = ""
self.lastname = ""
self.email = ""

def set_email(self, email):
print "in set email"
self.email = email

def get_email(self):
print "in get mail"
return "<" + self.email + ">"

email = property(get_email, set_email, None, 'Setting the email adress')

if __name__ == '__main__':
print "testing"

a = Address()
a.firstname = "gandalf"
a.email = "wiz...@middle-earth.com"
print a.firstname
print a.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??

I thought with properties there are no more endless recursion
maybe I just do not understand but I do not see an advantage right now over
__getattr__ and __setattr__ and __dict__

please help me to understand the new stuff.
is there something in the online documentation?? I couldn't find anythong
about properties

markus

Chris Liechti

unread,
May 23, 2002, 5:58:52 PM5/23/02
to
Markus Jais <in...@mjais.de> wrote in
news:acjnug$q6c68$1...@ID-75083.news.dfncis.de:

i think the usual way is to store the value in a variable with preceeding
underlines ("self._email" or two underlines if you want a private
attribute)



> I thought with properties there are no more endless recursion
> maybe I just do not understand but I do not see an advantage right now
> over __getattr__ and __setattr__ and __dict__

the advantage is that you can convert an data attribute later to execute
code through the getter/setter. you can do this per attribute, easier than
to mess with __getattr__ etc. which might be used for other things and
needs the __dict__ trick in __init__



> please help me to understand the new stuff.
> is there something in the online documentation?? I couldn't find
> anythong about properties

i think there is an example in the "what's new" document of 2.2 which you
find on python.org under the 2.2 releases&docs

chris


--
Chris <clie...@gmx.net>

Mike C. Fletcher

unread,
May 23, 2002, 6:04:24 PM5/23/02
to
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??

...

Markus Jais

unread,
May 23, 2002, 6:51:11 PM5/23/02
to
Chris Liechti wrote:

thanks for your tip: using an underline works
but when I use two underlines with slots like this

class Address(object):

__slots__ = ('firstname', 'lastname', __email')

def __init__(self):
self.firstname = ""
self.lastname = ""

self.__email = "


def set_email(self, email):
print "in set email"

self.__email = email

......

I get this error:

Traceback (most recent call last):
File "./Address.py", line 49, in ?
a = Address()
File "./Address.py", line 24, in __init__
self.__email = ""
AttributeError: 'Address' object has no attribute '_Address__email'


but with one underline, it works just fine.

markus

Chris Liechti

unread,
May 23, 2002, 7:04:53 PM5/23/02
to
Markus Jais <in...@mjais.de> wrote in
news:acjrga$q04ag$1...@ID-75083.news.dfncis.de:
> Chris Liechti wrote:

>> Markus Jais <in...@mjais.de> wrote:
>> i think the usual way is to store the value in a variable with
>> preceeding underlines ("self._email" or two underlines if you want a
>> private attribute)
>
> thanks for your tip: using an underline works
> but when I use two underlines with slots like this
>
> class Address(object):
> __slots__ = ('firstname', 'lastname', __email')
> def __init__(self):
> self.__email = "
> I get this error:

> AttributeError: 'Address' object has no attribute '_Address__email'

that's the way "private" attributes are created in python - by adding the
class name in from of them.

never used __slots__ yet, but maybe it would work when you write:
__slots__ = ('firstname', 'lastname', '_Address__email')

OTH making things private is not the prefered programming style in python.
i would prefer one underline (that means by convention "that's meant to be
internal, don't touch it", but it's still easy if you have to).

chris


--
Chris <clie...@gmx.net>

0 new messages