Overwriting property-> can't set attribute

Discussion in 'Python' started by Gregor Horvath, Aug 22, 2008.

  1. Hi,

    why is this code failing?

    class B(object):
    pass

    B.testattr = property(lambda s:"hallo")
    b = B()
    b.testattr = "test"


    Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)

    /tmp/python-14202ViU.py in <module>()
    14 B.testattr = property(lambda s:"hallo")
    15 b = B()
    ---> 16 b.testattr = "test"
    17
    18

    <type 'exceptions.AttributeError'>: can't set attribute

    --
    Greg
     
    Gregor Horvath, Aug 22, 2008
    #1
    1. Advertising

  2. Gregor Horvath schrieb:
    >
    > why is this code failing?


    OK I answer myself :)

    Because there is not fset function definied in the property.
    I have to del the attr before rebinding the attributename to another object.

    --
    Greg
     
    Gregor Horvath, Aug 22, 2008
    #2
    1. Advertising

  3. On Aug 22, 5:38 am, Gregor Horvath <> wrote:
    > why is this code failing?
    >
    > class B(object):
    >      pass
    >
    > B.testattr = property(lambda s:"hallo")
    > b = B()
    > b.testattr = "test"


    First, property() only works when attached to classes, not instances.
    So the assignment should be: B.testattr = property(...)

    Second, the property() call in you example only defines a getter
    function (the first argument) and not a setter function (the second
    argument) it defaults to a read-only property. That is why
    b.testattr='test' would still fail.

    Raymond
     
    Raymond Hettinger, Aug 22, 2008
    #3
  4. Raymond Hettinger a écrit :
    > On Aug 22, 5:38 am, Gregor Horvath <> wrote:
    >> why is this code failing?
    >>
    >> class B(object):
    >> pass
    >>
    >> B.testattr = property(lambda s:"hallo")
    >> b = B()
    >> b.testattr = "test"

    >
    > First, property() only works when attached to classes, not instances.
    > So the assignment should be: B.testattr = property(...)


    Mmm... You may want to reread the OP code more carefully !-)
     
    Bruno Desthuilliers, Aug 22, 2008
    #4
  5. Gregor Horvath

    norseman Guest

    Gregor Horvath wrote:
    > Hi,
    >
    > why is this code failing?
    >
    > class B(object):
    > pass
    >
    > B.testattr = property(lambda s:"hallo")
    > b = B()
    > b.testattr = "test"
    >
    >
    > Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
    >
    > /tmp/python-14202ViU.py in <module>()
    > 14 B.testattr = property(lambda s:"hallo")
    > 15 b = B()
    > ---> 16 b.testattr = "test"
    > 17
    > 18
    >
    > <type 'exceptions.AttributeError'>: can't set attribute
    >
    > --
    > Greg
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >

    ====================================

    b = B() # synonyms
    When B.testattr = "hallo" so does b.testattr
    So if in subsequent code:
    B.testattr = property(lambda s:"test")
    Then:
    b.testattr yields "test"
    unless b was essentially destroyed/reused in between times


    this is how/why things like:
    gc = damn-lot-of-typing-due-to-long-names
    gc.something
    works as if it was:
    damn-lot-of-typing-due-to-long-names.something


    Steve
     
    norseman, Aug 22, 2008
    #5
  6. norseman a écrit :
    > Gregor Horvath wrote:
    >> Hi,
    >>
    >> why is this code failing?
    >>
    >> class B(object):
    >> pass
    >>
    >> B.testattr = property(lambda s:"hallo")
    >> b = B()
    >> b.testattr = "test"
    >>
    >>
    >> Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
    >>
    >> /tmp/python-14202ViU.py in <module>()
    >> 14 B.testattr = property(lambda s:"hallo")
    >> 15 b = B()
    >> ---> 16 b.testattr = "test"
    >> 17
    >> 18
    >>
    >> <type 'exceptions.AttributeError'>: can't set attribute
    >>
    >> --
    >> Greg
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>

    > ====================================
    >
    > b = B() # synonyms


    Not exactly, no. You probably missed the call operator applied to B.

    (snip erroneous explanation).
     
    Bruno Desthuilliers, Aug 26, 2008
    #6
  7. Gregor Horvath a écrit :
    > Hi,
    >
    > why is this code failing?
    >
    > class B(object):
    > pass
    >
    > B.testattr = property(lambda s:"hallo")
    > b = B()
    > b.testattr = "test"
    >
    >
    > Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
    >
    > /tmp/python-14202ViU.py in <module>()
    > 14 B.testattr = property(lambda s:"hallo")
    > 15 b = B()
    > ---> 16 b.testattr = "test"
    > 17
    > 18
    >
    > <type 'exceptions.AttributeError'>: can't set attribute
    >


    It's not failing, it's doing exactly what's expected. You made testattr
    a read-only property by not providing a setter.
     
    Bruno Desthuilliers, Aug 26, 2008
    #7
    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. Joshua Beall
    Replies:
    1
    Views:
    473
    Bertilo Wennergren
    Dec 10, 2003
  2. Noozer
    Replies:
    10
    Views:
    2,237
    Mitja
    Oct 13, 2004
  3. jb
    Replies:
    1
    Views:
    4,465
  4. Stan B
    Replies:
    3
    Views:
    569
    Stan B
    Apr 6, 2007
  5. Cameron Eckman

    Can you set a "required attribute" on control property?

    Cameron Eckman, Feb 20, 2007, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    428
    Cameron Eckman
    Feb 20, 2007
Loading...

Share This Page