Different values for property vs attribute???

J

j_mckitrick

This has really got me confused:

class Klass:
def __init__(self):
self.mvar = 11

def getvar(self):
return self.mvar

def setvar(self, v):
self.mvar = v

pvar = property(getvar, setvar)

k = Klass()
print 'member var is %d' % k.mvar
print 'property var is %d' % k.pvar
k.pvar = 22
print 'member var is %d' % k.mvar
print 'property var is %d' % k.pvar

----------------
This gives the output:
member var is 11
property var is 11
member var is 11
property var is 22

---------------------------

What am I missing? Isn't the property shortcut just an accessor to
the same member variable???

jonathon
 
R

Russell Blau

j_mckitrick said:
This has really got me confused:

class Klass:
def __init__(self): ....
What am I missing? Isn't the property shortcut just an accessor to
the same member variable???

As someone else pointed out in another thread within the last 24 hours,
properties are only supported for new-style classes. Try changing the first
line to

class Klass(object):

and see if it works.
 
J

j_mckitrick

As someone else pointed out in another thread within the last 24 hours,
properties are only supported for new-style classes. Try changing the first
line to

class Klass(object):

and see if it works.

That was it, thanks. Sorry for the redundant question, but didn't
know (object) was necessary. I thought the new interpreter added
those qualities automagically.

thanks again
 
G

Greg Ewing

Russell said:
As someone else pointed out in another thread within the last 24 hours,
properties are only supported for new-style classes. Try changing the first
line to

class Klass(object):

and see if it works.

Since this seems to be such an easy error to make, and
the symptoms are so obscure, is there any way the
interpreter could be made to check for it?

E.g. maybe old-style classes could refuse to accept any
attribute which is an instance of property.
 
J

j_mckitrick

I forgot to mention, I have another class like this:

def getauditcheckpass(self):
return self.state[AUDITCHK] == PASS

# properties
auditcheckpass = property(getauditcheckpass)


and it seemed to work ok.
 
S

Shalabh Chaturvedi

j_mckitrick said:
I forgot to mention, I have another class like this:

def getauditcheckpass(self):
return self.state[AUDITCHK] == PASS

# properties
auditcheckpass = property(getauditcheckpass)


and it seemed to work ok.

From http://www.python.org/2.2.3/descrintro.html#property :

Properties do not work for classic classes, but you don't get a clear
error when you try this. Your get method will be called, so it appears
to work, but upon attribute assignment, a classic class instance will
simply set the value in its __dict__ without calling the property's set
method...
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top