property () for Java Programmers ?

M

michael

Hi there,

I am somewhat confused by the following :

class C(object):
def getx(self): return self.__x
def setx(self, value): self.__x = "extended" + value
def delx(self): del self.__x
x = property(getx, setx, delx, "I'm the 'x' property.")

So far so good :) But what to do with this now
['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__', 'delx', 'getx', 'setx', 'x']
?????? What can I do with this "property object" now.

Confused greetings

Michael
 
S

Steven Bethard

michael said:
Hi there,

I am somewhat confused by the following :

class C(object):
def getx(self): return self.__x
def setx(self, value): self.__x = "extended" + value
def delx(self): del self.__x
x = property(getx, setx, delx, "I'm the 'x' property.")

So far so good :) But what to do with this now


['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__', 'delx', 'getx', 'setx', 'x']

<property object at 0x401edbbc>


?????? What can I do with this "property object" now.

Well, if you actually want your getx/setx/delx to be called, then you
need an *instance* of class C:

py> c = C()
py> c.x
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 2, in getx
AttributeError: 'C' object has no attribute '_C__x'
py> c.x = "42"
py> c.x
'extended42'
py> del c.x
py> c.x
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 2, in getx
AttributeError: 'C' object has no attribute '_C__x'

Note that I used 'c = C()' instead of 'c = C' as in your code.

STeve
 
M

michael.bierenfeld

Note that I used 'c = C()' instead of 'c = C' as in your code.
Hello,

thanks that was the problem. *hmpf* :)

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top