R
Ruud de Jong
I have the situation where I need to construct the name
of a static method, and then retrieve the corresponding
function from a class object.
I thought I could just use __getattribute__ for this purpose.
This works fine if I already have an instantiation of the class,
but not when I try this on the class object directly.
A bare bones example:
def a():
print "static method a"
a = staticmethod(a)
It works for a instantiation of class C:
static method a
But for the class object it works differently:
Traceback (most recent call last):
File "<pyshell#219>", line 1, in -toplevel-
C.__getattribute__(C,'a')()
TypeError: 'staticmethod' object is not callable
After some experimentation and documentation searching,
I found that to get at the actual function, the __get__
method for the staticmethod descriptor must be called:
static method a
If I use an class instance as the first argument
it works OK. But I ran into this problem when I tried
to use __getattribute__ in the __new__ function of a class
-- there is no class instance yet at that point,
and calling C() there leads to infinite recursion.
Another variant that worked is to call __getattribute__
static method a
But according to section 3.3.2.1 of the Language Reference
(More attribute access for new-style classes),
__getattribute__ "... should return the (computed) attribute value".
This could be interpreted to say that __getattribute__ should
return the function, not the staticmethod object.
Is there a reason for this difference in behavior?
of a static method, and then retrieve the corresponding
function from a class object.
I thought I could just use __getattribute__ for this purpose.
This works fine if I already have an instantiation of the class,
but not when I try this on the class object directly.
A bare bones example:
def a():
print "static method a"
a = staticmethod(a)
It works for a instantiation of class C:
static method a
But for the class object it works differently:
Traceback (most recent call last):
File "<pyshell#219>", line 1, in -toplevel-
C.__getattribute__(C,'a')()
TypeError: 'staticmethod' object is not callable
After some experimentation and documentation searching,
I found that to get at the actual function, the __get__
method for the staticmethod descriptor must be called:
static method a
If I use an class instance as the first argument
it works OK. But I ran into this problem when I tried
to use __getattribute__ in the __new__ function of a class
-- there is no class instance yet at that point,
and calling C() there leads to infinite recursion.
Another variant that worked is to call __getattribute__
static method a
But according to section 3.3.2.1 of the Language Reference
(More attribute access for new-style classes),
__getattribute__ "... should return the (computed) attribute value".
This could be interpreted to say that __getattribute__ should
return the function, not the staticmethod object.
Is there a reason for this difference in behavior?