473,461 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Static method object not callable

This simple code example gives me the message, "TypeError: 'staticmethod'
object is not callable".

class X(object):
def Y(x):
print x
Y = staticmethod(Y)
ad = { 1 : Y }
def Z(self):
self.ad[1](3)
x = X()
x.Z()
print "Done."

I know there must be a way to have a class attribute reference a static
method, and then call that static method through the reference, so if anyone
can correct this it would be appreciated.

Jul 18 '05 #1
7 15557
Edward Diener wrote:
This simple code example gives me the message, "TypeError: 'staticmethod'
object is not callable".

class X(object):
def Y(x):
print x
Y = staticmethod(Y)
ad = { 1 : Y }
def Z(self):
self.ad[1](3)
x = X()
x.Z()
print "Done."

I know there must be a way to have a class attribute reference a static
method, and then call that static method through the reference, so if anyone
can correct this it would be appreciated.


Here are two alternatives.

class X(object):
def Y(x):
print x
Y = staticmethod(Y)
ad = { 1 : 'Y' }
def Z(self):
getattr(self, self.ad[1])(3)
x = X()
x.Z()
print "Done."
class X(object):
def Y(x):
print x
Y = staticmethod(Y)
def __init__(self):
self.ad = { 1 : self.Y }
def Z(self):
self.ad[1](3)
x = X()
x.Z()
print "Done."

Jul 18 '05 #2
Paul Morrow wrote:
Edward Diener wrote:
This simple code example gives me the message, "TypeError:
'staticmethod' object is not callable".

class X(object):
def Y(x):
print x
Y = staticmethod(Y)
ad = { 1 : Y }
def Z(self):
self.ad[1](3)
x = X()
x.Z()
print "Done."

I know there must be a way to have a class attribute reference a
static method, and then call that static method through the
reference, so if anyone can correct this it would be appreciated.


Here are two alternatives. snip...


Neither alternative is satisfactory. Surely there must be some means of
directly specifying a reference to a static method in a class attribute in
Python, and calling it from an instance method through the class attribute.
Jul 18 '05 #3
Edward Diener wrote:
This simple code example gives me the message, "TypeError: 'staticmethod'
object is not callable".

class X(object):
def Y(x):
print x
Y = staticmethod(Y)
ad = { 1 : Y }
def Z(self):
self.ad[1](3)
x = X()
x.Z()
print "Done."

I know there must be a way to have a class attribute reference a static
method, and then call that static method through the reference, so if
anyone can correct this it would be appreciated.


A staticmethod has to be accessed from the class or an instance. But you can
put the 'bare' function in your dictionary before you make a staticmethod
out of it:

class X(object):
def Y(x):
print x
ad = { 1 : Y }
Y = staticmethod(Y)
def Z(self):
self.ad[1](3)
x = X()
x.Z()
print "Done."

This prints:

3
Done.

HTH,
Shalabh

Jul 18 '05 #4
On Wed, 11 Aug 2004 02:40:07 GMT, "Edward Diener" <el******@earthlink.net> wrote:
Paul Morrow wrote:
Edward Diener wrote:
This simple code example gives me the message, "TypeError:
'staticmethod' object is not callable".

class X(object):
def Y(x):
print x
Y = staticmethod(Y)
ad = { 1 : Y }
def Z(self):
self.ad[1](3)
x = X()
x.Z()
print "Done."

I know there must be a way to have a class attribute reference a
static method, and then call that static method through the
reference, so if anyone can correct this it would be appreciated. By 'reference' are you referring to the Y in ad?


Here are two alternatives. snip...


Neither alternative is satisfactory. Surely there must be some means of
directly specifying a reference to a static method in a class attribute in
Python, and calling it from an instance method through the class attribute.

I'm not sure what you really want, but a staticmethod object is UIAM a descriptor
and when descriptors are retrieved as attributes of something, their __get__ methods
are called. That's why getattr was part of the other alternatives. Alternatively,
you can call the __get__ method directly, and in the case of staticmethod, that
will get you back the unadorned function in question, which you can then call. E.g.,
class X(object): ... def Y(x):
... print x
... Y = staticmethod(Y)
... ad = { 1 : Y }
... def Z(self):
... self.ad[1].__get__(self, type(self))(3)
... x = X()
x.Z() 3

(BTW, for staticmethod.__get__, probably any argument(s)
other than None or None,None will work)

Maybe this will clarify a little:

A function also works like a descriptor, and has a __get__ method, which is called
to create a bound method when the function is retrieved as an attribute of an
object instance. Note that Z is a function with one argument (self).

When retrieving Z as attribute of the class, the __get__ method gets
the arguments __get__(None, X), which gets you the unbound method.
X.Z <unbound method X.Z>

If we bypass the attribute access mechanism, we get the function itself: X.__dict__['Z'] <function Z at 0x009062B0>

If we do the getattr mechanics manually, calling the __get__ of the function,
we see the method results corresponding to X.Z and x.Z respectively:
X.__dict__['Z'].__get__(None, X) <unbound method X.Z> X.__dict__['Z'].__get__(x, X) <bound method X.Z of <__main__.X object at 0x009011D0>>

We can make a hokey descriptor object to show this: class Desc(object): ... def __get__(*args): print args
... class C(object): ... attr = Desc()
... c = C()
C.attr (<__main__.Desc object at 0x009015D0>, None, <class '__main__.C'>) c.attr (<__main__.Desc object at 0x009015D0>, <__main__.C object at 0x00901950>, <class '__main__.C'>)

So the __get__ args are the self of the decriptor instance itself (handy for carrying baggage like
a function), and here either None,C or c,C for C.attr and c.attr repectively.

Y is a staticmethod object though, not a plain function.
The __get__ method of a staticmethod object returns the same thing
(the original function) for either kind of call.
So when retrieved as an attribute of something, you get the function in question:
X.Y <function Y at 0x008FDF70> x.Y <function Y at 0x008FDF70>

Bypassing attribute mechanism: X.__dict__['Y'] <staticmethod object at 0x00901110>

Doing it manually bound-method style or unbound-method style: X.__dict__['Y'].__get__(x, X) <function Y at 0x008FDF70> X.__dict__['Y'].__get__(None, X) <function Y at 0x008FDF70>

Calling the retrieved function: X.__dict__['Y'].__get__(x, X)(123) 123

Of course, X.Y(456) 456 x.Y(456) 456

or getattr(X,'Y')(789) 789 getattr(x,'Y')(789)

789

I still don't know what you are really trying to do with that
Z method and the ad dict ;-)

Regards,
Bengt Richter
Jul 18 '05 #5
Edward Diener wrote:
This simple code example gives me the message, "TypeError: 'staticmethod'
object is not callable".

[snip]

I know there must be a way to have a class attribute reference a static
method, and then call that static method through the reference, so if anyone
can correct this it would be appreciated.


class X(object):
def Y(x):
print x
Y = staticmethod(Y)
def Z(self):
self.ad[1](3)
X.ad = {1: X.Y}

Jp

Jul 18 '05 #6
Bengt Richter wrote:
On Wed, 11 Aug 2004 02:40:07 GMT, "Edward Diener"
<el******@earthlink.net> wrote:
Paul Morrow wrote:
Edward Diener wrote:
This simple code example gives me the message, "TypeError:
'staticmethod' object is not callable".

class X(object):
def Y(x):
print x
Y = staticmethod(Y)
ad = { 1 : Y }
def Z(self):
self.ad[1](3)
x = X()
x.Z()
print "Done."

I know there must be a way to have a class attribute reference a
static method, and then call that static method through the
reference, so if anyone can correct this it would be appreciated. By 'reference' are you referring to the Y in ad?

Yes. I understand now why it won't work but I think this is a limitation of
Python. Evidently, if I set up my class attribute 'ad' to be "ad = { 1 :
X.Y }", then calling static method X.Y through "self.ad[1](3)" would work.
But Python does not allow me to say 'X.Y' from within a class attribute of X
because X has not been fully defined as a class at that time. As one person
answered, I can say "X.ad = { 1 : X.Y }" after the definition of class X and
that works fine. But that is very kludgy to me, forcing me to wait until the
definition of X is finished in order to create my ad class attribute.

I thank you very much for the explanation of _get_ and how static methods
work in Python. It still feels unnatural not to be able to reference a class
attribute of a class in another class attribute of the same class from
within the class definition.
Jul 18 '05 #7
On Thu, 12 Aug 2004 02:07:31 GMT, "Edward Diener" <el******@earthlink.net> wrote:
Bengt Richter wrote:
On Wed, 11 Aug 2004 02:40:07 GMT, "Edward Diener"
<el******@earthlink.net> wrote:
Paul Morrow wrote:
Edward Diener wrote:
> This simple code example gives me the message, "TypeError:
> 'staticmethod' object is not callable".
>
> class X(object):
> def Y(x):
> print x
> Y = staticmethod(Y)
> ad = { 1 : Y }
> def Z(self):
> self.ad[1](3)
> x = X()
> x.Z()
> print "Done."
>
> I know there must be a way to have a class attribute reference a
> static method, and then call that static method through the
> reference, so if anyone can correct this it would be appreciated.

By 'reference' are you referring to the Y in ad?
>

Yes. I understand now why it won't work but I think this is a limitation of
Python. Evidently, if I set up my class attribute 'ad' to be "ad = { 1 :
X.Y }", then calling static method X.Y through "self.ad[1](3)" would work.
But Python does not allow me to say 'X.Y' from within a class attribute of X
because X has not been fully defined as a class at that time. As one person
answered, I can say "X.ad = { 1 : X.Y }" after the definition of class X and
that works fine. But that is very kludgy to me, forcing me to wait until the
definition of X is finished in order to create my ad class attribute.

I thank you very much for the explanation of _get_ and how static methods
work in Python. It still feels unnatural not to be able to reference a class
attribute of a class in another class attribute of the same class from
within the class definition.

Ok, got a couple things done. Reward time = play with python a little ;-)

Since ad is an attribute, you could give *it* some magic, if you wanted
to get your effect. Taking a cue from staticmethod and property etc, we'll
call it dienerize, and you use it to dienerize a dict class variable, analogous
to method = staticmethod(method). Then dict values that are descriptors will be
called to see what reading them as attributes would produce. Non-descriptor dict
values just come through plain. I added a 2:'ordinary' example in the dict, and
3:Z to refer to an ordinary method, which is a function-valued class variable, but
functions all have __get__ methods, so they become transformed when they are
accessed as attributes (or the equivalent magic). I had do move the ad definition
so the Z name reference was valid.

I'm living with your capitalized method names through gritted teeth ;-)
Also added X.W to help show some effects. Don't take this as gospel, it's
the first time I tried this twist on a descriptor ;-)

----< diener.py >--------------------
#diener.py
class dienerize(dict):
def __getitem__(self, key):
value = dict.__getitem__(self, key)
if not hasattr(value, '__get__'): return value
return value.__get__(*self.getargs)
def __get__(self, *getargs):
self.getargs = getargs
return self # which should act like magic dict

class X(object):
def Y(x):
print x
Y = staticmethod(Y)
def Z(self):
self.ad[1](3)
ad = { 1 : Y, 2: 'ordinary', 3: Z }
ad = dienerize(ad)
def W(self, *args):
for i,arg in enumerate(args):
self.ad[1]('arg %2s: %r'%(i,arg)) # use Y for printing ;-)
x = X()
x.Z()
x.W(1,2,3)
x.W(X.Y, X.__dict__['Y'], x.ad[2], X.ad[2])
x.W(X.ad, X.__dict__['ad'], x.ad[1], X.ad[1])
x.W(x.ad[1], x.ad[2], x.ad[3]) # note that 3:Z becomes bound
x.W(x.ad[3],' ^--x.ad[3]',X.ad[3],' ^--X.ad[3]', x.__class__.__dict__['Z'],
" ^--x.__class__.__dict__['Z']")
print "Done."
-------------------------------------

Running it:

[22:18] C:\pywk\sovm>diener.py
3
arg 0: 1
arg 1: 2
arg 2: 3
arg 0: <function Y at 0x008FDEB0>
arg 1: <staticmethod object at 0x009013B0>
arg 2: 'ordinary'
arg 3: 'ordinary'
arg 0: {1: <staticmethod object at 0x009013B0>, 2: 'ordinary', 3: <function Z at 0x008FDEF0>}
arg 1: {1: <staticmethod object at 0x009013B0>, 2: 'ordinary', 3: <function Z at 0x008FDEF0>}
arg 2: <function Y at 0x008FDEB0>
arg 3: <function Y at 0x008FDEB0>
arg 0: <function Y at 0x008FDEB0>
arg 1: 'ordinary'
arg 2: <bound method X.Z of <__main__.X object at 0x00901390>>
arg 0: <bound method X.Z of <__main__.X object at 0x00901390>>
arg 1: ' ^--x.ad[3]'
arg 2: <unbound method X.Z>
arg 3: ' ^--X.ad[3]'
arg 4: <function Z at 0x008FDEF0>
arg 5: " ^--x.__class__.__dict__['Z']"
Done.

Since you now know why the original "didn't work" I figure you will know
why this one appears to ;-)

Gotta go.

Regards,
Bengt Richter
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
2
by: Tony Johansson | last post by:
Hello Experts! I know that you can't have virtual static methods and I know what a static method is. A static method exist only one time no matter how many object you have. You have this...
4
by: DDE | last post by:
Hi All, Reformulating a problem from a previous post: How can I access an application variable from a static method? When I try: string myString = Application{"thisString"].ToString; ...
1
by: Greif | last post by:
I am trying to call a static method within a sealed class. However, the wrong method keeps getting called instead. In a seperate class I am calling the following code: LogHelper.Log("Sample...
8
by: Fernando Lopes | last post by:
Hi there! Someone has some code sample about when is recommend use a statis method? I know this methos don't want to be initialized and all but I want to know when I need to use it. Tks....
3
by: Bob | last post by:
I have an abstract class Thing which has a static method Thing GetThing(). Class Something inherits from Thing. SomeThing supplies static info for GetThing to get stuff from the database to create...
4
by: David++ | last post by:
Hi folks, I am building a simple app which uses a splash Screen. As there will only be one instance of the Splash Screen ever running at one time I am thinking that it makes sense to make the...
3
by: =?Utf-8?B?SmltSGVhdmV5?= | last post by:
I want to invoke a static method of a static class using a TEXT variable which has the namespace and class name which should be executed. I looked at the "CreateInstance" method, but this looks...
5
by: DamienS | last post by:
Hi, I have a static method in a class and I need to be able to return a reference to "this". Googling around, I found a heap of discussions of the pros/cons of "abstract static" etc. It was...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.