[Tutor] property built-in
Python
python at venix.com
Thu Oct 19 19:37:10 CEST 2006
On Thu, 2006-10-19 at 19:03 +0200, thomas wrote:
> Hi,
>
>
> I was wondering some things about property.
>
> suppose I have a class like this:
>
> class A(object):
> def __init__(self, x, y):
> self.__x = x
>
> def x():
> def get(self):
fget
> return self.__x
>
> def set(self, x):
fset
> self.__x = x
> #and some other code that is important
return locals()
>
> x= property(**x())
>
> questions:
> 1: why doesn't there have to be a self in x()?
x is not a "normal" class method. It is only used as a container to
hold the "real" methods which are returned in the locals() dictionary.
property supports keyword arguments fget, fset, fdel and doc. The
definitions in your x method (Too many uses of x for different purposes)
MUST match the property keywords because of the use of locals().
> 2: how do I address the setter of x in the init?
Just use it. Change __init__ to have
self.x = x
By the time __init__ is usable,
x = property( **x())
will have done its magic. x will be a property of the class.
>>> class A(object):
... def __init__(self, x, y):
... self.x = x
... def x():
... def fget(self):
... return self.__x
... def fset(self, x):
... self.__x = x
... #and some other code that is important
... return locals()
... x= property(**x())
...
>>> a = A(1,2)
>>> a.x
1
>>> a.x=4
>>> a.x
4
>
> I think these are basic questions, but I can't seem to find out how to
> do it.
>
>
> Thanks
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Lloyd Kvam
Venix Corp
More information about the Tutor
mailing list