'classmethod' object has only read-only attributes

Discussion in 'Python' started by Thomas Guettler, Nov 25, 2009.

  1. Hi,

    why have classmethods only readonly attributes? It works for other methods.

    exmpale code:
    {{{
    class Foo(object):
    @classmethod
    def bar(cls):
    pass
    bar.myattr='test'
    }}}

    user@host:~> python ~/tmp/t.py
    Traceback (most recent call last):
    File "/home/user/tmp/t.py", line 1, in <module>
    class Foo(object):
    File "/home/user/tmp/t.py", line 5, in Foo
    bar.myattr='test'
    TypeError: 'classmethod' object has only read-only attributes (assign to .myattr)


    --
    Thomas Guettler, http://www.thomas-guettler.de/
    E-Mail: guettli (*) thomas-guettler + de
     
    Thomas Guettler, Nov 25, 2009
    #1
    1. Advertisements

  2. Thomas Guettler

    Peter Otten Guest

    Thomas Guettler wrote:

    > Hi,
    >
    > why have classmethods only readonly attributes? It works for other
    > methods.
    >
    > exmpale code:
    > {{{
    > class Foo(object):
    > @classmethod
    > def bar(cls):
    > pass
    > bar.myattr='test'
    > }}}
    >
    > user@host:~> python ~/tmp/t.py
    > Traceback (most recent call last):
    > File "/home/user/tmp/t.py", line 1, in <module>
    > class Foo(object):
    > File "/home/user/tmp/t.py", line 5, in Foo
    > bar.myattr='test'
    > TypeError: 'classmethod' object has only read-only attributes (assign to
    > .myattr)


    No idea. But here's a workaround:

    >>> class A(object):

    .... def method(cls): print cls
    .... method.foo = 42
    .... method = classmethod(method)
    ....
    >>> A.method()

    <class '__main__.A'>
    >>> A.method.foo

    42

    Or, going fancy:

    >>> def attrs(**kw):

    .... def set(obj):
    .... for k, v in kw.iteritems():
    .... setattr(obj, k, v)
    .... return obj
    .... return set
    ....
    >>> class A(object):

    .... @classmethod
    .... @attrs(foo=42)
    .... def method(cls): print cls
    ....
    >>> A.method()

    <class '__main__.A'>
    >>> A().method.foo

    42

    Peter
     
    Peter Otten, Nov 25, 2009
    #2
    1. Advertisements

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. keithb
    Replies:
    2
    Views:
    8,374
    keithb
    Jun 7, 2006
  2. Paul Moore
    Replies:
    9
    Views:
    807
    Roy Smith
    Feb 21, 2005
  3. Samuel M. Smith
    Replies:
    6
    Views:
    435
    Samuel M. Smith
    Dec 9, 2005
  4. limodou

    Re: read-only attributes

    limodou, Feb 10, 2006, in forum: Python
    Replies:
    1
    Views:
    453
    bruno at modulix
    Feb 10, 2006
  5. Tim Chase
    Replies:
    2
    Views:
    417
    James Stroud
    Mar 15, 2006
  6. crystalattice
    Replies:
    2
    Views:
    530
    crystalattice
    Sep 1, 2006
  7. TechieInsights
    Replies:
    5
    Views:
    525
    josh logan
    Feb 12, 2009
  8. Mark Pelletier

    dynamically read object's attributes

    Mark Pelletier, Sep 21, 2007, in forum: Ruby
    Replies:
    4
    Views:
    219
    Jano Svitok
    Sep 21, 2007
Loading...