Overwriting property-> can't set attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gregor Horvath

    Overwriting property-> can't set attribute

    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.Att ributeError'>: can't set attribute

    --
    Greg
  • Gregor Horvath

    #2
    Re: Overwriting property-&gt; can't set attribute

    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

    Comment

    • Raymond Hettinger

      #3
      Re: Overwriting property-&gt; can't set attribute

      On Aug 22, 5:38 am, Gregor Horvath <g...@gregor-horvath.comwrot e:
      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='tes t' would still fail.

      Raymond

      Comment

      • Bruno Desthuilliers

        #4
        Re: Overwriting property-&gt; can't set attribute

        Raymond Hettinger a écrit :
        On Aug 22, 5:38 am, Gregor Horvath <g...@gregor-horvath.comwrot e:
        >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 !-)

        Comment

        • norseman

          #5
          Re: Overwriting property-&gt; can't set attribute

          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.Att ributeError'>: can't set attribute
          >
          --
          Greg
          --

          >
          =============== =============== ======

          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@hughes .net

          Comment

          • Bruno Desthuilliers

            #6
            Re: Overwriting property-&gt; can't set attribute

            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.Att ributeError'>: 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).

            Comment

            • Bruno Desthuilliers

              #7
              Re: Overwriting property-&gt; can't set attribute

              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.Att ributeError'>: 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.

              Comment

              Working...