Very, very strange problem with properties

Discussion in 'Python' started by Kenneth McDonald, May 4, 2004.

  1. For some reason, properties seem to have stopped working correctly
    on my system. (Mac OS X 10.3, custom-compiled Python 2.3.3) To the
    best of my knowledge, they were working correctly at some point
    in the not-too-distant past. The confusing thing is that they
    _partly_ work.

    Consider the following code:

    class foo:
    def __init__(self):
    self._x = 3

    def __setx(self, val):
    print "Calling __setx"
    pass

    x = property(fget=lambda self: self._x, fset=__setx)

    f = foo()
    print f.__dict__
    print f.x
    f.x = 2
    print f.x
    f._x = 7
    print f.x
    print f.__dict__


    Running this _should_ print (ignoring the dict printouts)
    3
    Calling __setx
    3
    7
    since the __setx function does nothing. In fact, what I
    get (dicts included) is:

    {'_x': 3}
    3
    2
    2
    {'x': 2, '_x': 7}

    i.e. the assignment f.x = 2 is overwriting the property!
    If I define the property as

    x = property(fget=lambda self: self._x)

    then running the code should cause an error on f.x = 7;
    in fact, I get the same result as when the fset is
    defined as __setx.

    So, it seems that the "getter" aspect of properties is
    working, but the "setter" aspect is ignored in such a way
    that regular attribute assignment takes place. I find
    this very, very strange.

    I would prefer to avoid reinstalling Python on my system,
    since keeping a custom Python working on OS X is a bit
    of a gamble anyway, at this point, but will if I have to.
    But I would prefer it if someone could suggest other
    alternatives to try first, and frankly, I'm also just
    curious as to what could cause an oddity like this.


    Thanks,
    Ken McDonald
     
    Kenneth McDonald, May 4, 2004
    #1
    1. Advertising

  2. Kenneth McDonald

    Carl Banks Guest

    Kenneth McDonald wrote:
    >
    >
    > For some reason, properties seem to have stopped working correctly
    > on my system. (Mac OS X 10.3, custom-compiled Python 2.3.3) To the
    > best of my knowledge, they were working correctly at some point
    > in the not-too-distant past. The confusing thing is that they
    > _partly_ work.
    >
    > Consider the following code:
    >
    > class foo:



    There's your problem. You've defined a classic class. Properties
    only work with new-style classes, so you need to use "class
    foo(object)".


    suspects-he-will-have-realized-this-2-seconds-after-posting-ly yr's,


    --
    CARL BANKS http://www.aerojockey.com/software
    "If you believe in yourself, drink your school, stay on drugs, and
    don't do milk, you can get work."
    -- Parody of Mr. T from a Robert Smigel Cartoon
     
    Carl Banks, May 4, 2004
    #2
    1. Advertising

  3. Urgh, and that explains why it was working before: I changed my class's
    parent class from 'object' to another classic class. Sigh.

    Still, I find it kinda strange that the getter worked...


    Thanks for the help!
    Ken

    In article <LHElc.13152$>, Carl Banks wrote:
    > Kenneth McDonald wrote:
    >>
    >>
    >> For some reason, properties seem to have stopped working correctly
    >> on my system. (Mac OS X 10.3, custom-compiled Python 2.3.3) To the
    >> best of my knowledge, they were working correctly at some point
    >> in the not-too-distant past. The confusing thing is that they
    >> _partly_ work.
    >>
    >> Consider the following code:
    >>
    >> class foo:

    >
    >
    > There's your problem. You've defined a classic class. Properties
    > only work with new-style classes, so you need to use "class
    > foo(object)".
    >
    >
    > suspects-he-will-have-realized-this-2-seconds-after-posting-ly yr's,
    >
    >
     
    Kenneth McDonald, May 4, 2004
    #3
    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. Shawn

    Very very strange. Major bug?

    Shawn, Feb 3, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    357
    Shawn
    Feb 4, 2004
  2. brewman
    Replies:
    0
    Views:
    1,452
    brewman
    Aug 28, 2003
  3. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    501
    Raymond Hettinger
    Jul 27, 2003
  4. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,687
    Merrill & Michele
    Oct 19, 2004
  5. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    448
    jacob navia
    Oct 17, 2004
Loading...

Share This Page