error: 'staticmethod' object is not callable

Discussion in 'Python' started by Michal Vitecek, Feb 11, 2004.

  1. hello everyone,

    today i've come upon a strange exception, consider the following file
    test.py:

    --- beginning of test.py ---

    class A(object):
    def method1(parA):
    print "in A.method1()"
    method1 = staticmethod(method1)

    def method2(parA, parB):
    print "in A.method2()"
    method1 = staticmethod(method1)
    # see (*)

    A.method1("some value")

    --- end of test.py ---

    when test.py is run, the following error is printed:

    $ python test.py
    Traceback (most recent call last):
    File "test.py", line 10, in ?
    A.method1("some value")
    TypeError: 'staticmethod' object is not callable
    $

    isn't this a bug somewhere in python? this was tested on 2.2.3.

    (*) this happened accidentaly by copy & paste error


    thank you,
    --
    fuf ()
     
    Michal Vitecek, Feb 11, 2004
    #1
    1. Advertising

  2. Michal Vitecek

    Peter Otten Guest

    Michal Vitecek wrote:

    > hello everyone,
    >
    > today i've come upon a strange exception, consider the following file
    > test.py:
    >
    > --- beginning of test.py ---
    >
    > class A(object):
    > def method1(parA):
    > print "in A.method1()"
    > method1 = staticmethod(method1)
    >
    > def method2(parA, parB):
    > print "in A.method2()"
    > method1 = staticmethod(method1)
    > # see (*)
    >
    > A.method1("some value")
    >
    > --- end of test.py ---
    >
    > when test.py is run, the following error is printed:
    >
    > $ python test.py
    > Traceback (most recent call last):
    > File "test.py", line 10, in ?
    > A.method1("some value")
    > TypeError: 'staticmethod' object is not callable
    > $
    >
    > isn't this a bug somewhere in python? this was tested on 2.2.3.
    >
    > (*) this happened accidentaly by copy & paste error


    You are wrapping method2() twice:

    class A(object):
    def method(parA):
    print "in A.method()"
    method = staticmethod(staticmethod(method))

    A.method("first")

    Why would you expect this to work?

    Peter
     
    Peter Otten, Feb 11, 2004
    #2
    1. Advertising

  3. Peter Otten wrote:
    >You are wrapping method2() twice:
    >
    >class A(object):
    > def method(parA):
    > print "in A.method()"
    > method = staticmethod(staticmethod(method))
    >
    >A.method("first")
    >
    >Why would you expect this to work?


    i don't expect this to work. i just don't get it why python allows this
    double wrapping of a method. it cannot be used for anything reasonable,
    can it?

    --
    fuf ()
     
    Michal Vitecek, Feb 11, 2004
    #3
  4. Michal Vitecek

    Peter Otten Guest

    Michal Vitecek wrote:

    > Peter Otten wrote:
    >>You are wrapping method2() twice:
    >>
    >>class A(object):
    >> def method(parA):
    >> print "in A.method()"
    >> method = staticmethod(staticmethod(method))
    >>
    >>A.method("first")
    >>
    >>Why would you expect this to work?

    >
    > i don't expect this to work. i just don't get it why python allows this
    > double wrapping of a method. it cannot be used for anything reasonable,
    > can it?
    >


    >>> import __builtin__
    >>> def staticmethod(m):

    .... assert callable(m), "staticmethod expects a callable as argument"
    .... return __builtin__.staticmethod(m)
    ....
    >>> class A(object):

    .... def method():
    .... pass
    .... method = staticmethod(method)
    .... method = staticmethod(method)
    ....
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<stdin>", line 5, in A
    File "<stdin>", line 2, in staticmethod
    AssertionError: staticmethod expects a callable as argument
    >>>


    Is this what you want then?
    Personally, I don't care much, because
    (1) I use static methods very rarely.
    (2) The error message makes it clear enough that something's wrong with a
    static method.

    If you think it's important, you could either use a wrapper function like
    above in your code or submit a patch.

    Peter
     
    Peter Otten, Feb 11, 2004
    #4
  5. Michal Vitecek <> writes:

    > Peter Otten wrote:
    > >You are wrapping method2() twice:
    > >
    > >class A(object):
    > > def method(parA):
    > > print "in A.method()"
    > > method = staticmethod(staticmethod(method))
    > >
    > >A.method("first")
    > >
    > >Why would you expect this to work?

    >
    > i don't expect this to work. i just don't get it why python allows this
    > double wrapping of a method. it cannot be used for anything reasonable,
    > can it?


    Hmm, in Python 2.3 the classmethod constructor checks its argument for
    callability, but the staticmethod constructor doesn't seem to. I
    guess this is just oversight...

    Cheers,
    mwh

    --
    59. In English every word can be verbed. Would that it were so in
    our programming languages.
    -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
     
    Michael Hudson, Feb 11, 2004
    #5
    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. Nicolas Fleury

    Make staticmethod objects callable?

    Nicolas Fleury, Feb 28, 2006, in forum: Python
    Replies:
    10
    Views:
    588
    Steven D'Aprano
    Mar 1, 2006
  2. Steven Bethard
    Replies:
    0
    Views:
    277
    Steven Bethard
    Mar 1, 2006
  3. exiquio
    Replies:
    2
    Views:
    550
    exiquio
    Oct 7, 2008
  4. Gabriel Genellina
    Replies:
    0
    Views:
    480
    Gabriel Genellina
    Nov 29, 2009
  5. Ulrich Eckhardt
    Replies:
    6
    Views:
    119
    Peter Otten
    Jul 12, 2013
Loading...

Share This Page