Different values for property vs attribute???

Discussion in 'Python' started by j_mckitrick, May 4, 2004.

  1. j_mckitrick

    j_mckitrick Guest

    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
    j_mckitrick, May 4, 2004
    #1
    1. Advertising

  2. j_mckitrick

    Russell Blau Guest

    "j_mckitrick" <> wrote in message
    news:...
    > 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.

    --
    I don't actually read my hotmail account, but you can replace hotmail with
    excite if you really want to reach me.
    Russell Blau, May 4, 2004
    #2
    1. Advertising

  3. j_mckitrick

    j_mckitrick Guest

    > 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
    j_mckitrick, May 5, 2004
    #3
  4. j_mckitrick

    Greg Ewing Guest

    Russell Blau wrote:
    > 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.

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
    Greg Ewing, May 5, 2004
    #4
  5. j_mckitrick

    j_mckitrick Guest

    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.
    j_mckitrick, May 5, 2004
    #5
  6. j_mckitrick wrote:
    > 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...

    --
    Shalabh
    Shalabh Chaturvedi, May 5, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    4
    Views:
    444
    Steve
    Jan 27, 2006
  2. Bostonasian
    Replies:
    1
    Views:
    816
    Joris Gillis
    Sep 18, 2005
  3. Lawrence
    Replies:
    16
    Views:
    537
  4. Replies:
    3
    Views:
    1,540
    Brandon McCombs
    May 2, 2007
  5. ociardhp
    Replies:
    6
    Views:
    710
    Owen Jacobson
    Oct 25, 2007
Loading...

Share This Page