By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

property () for Java Programmers ?

P: n/a
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
c = C
c <class '__main__.C'> dir (c) ['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__', 'delx', 'getx', 'setx', 'x'] c.x <property object at 0x401edbbc>


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

Confused greetings

Michael
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
michael wrote:
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

c = C
c
<class '__main__.C'>
dir (c)
['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__', 'delx', 'getx', 'setx', 'x']
c.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
Jul 18 '05 #2

P: n/a
> Note that I used 'c = C()' instead of 'c = C' as in your code.
Hello,

thanks that was the problem. *hmpf* :-)

Michael

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.