staticmethod and setattr

Michael.Lausch mick.lausch at
Mon Mar 15 09:43:02 CET 2010


I managed to get confused by Python, which is not such an easy task.

The problem i have is rooted in marshalling, JSON and Dojo.
I need some static class in function with the name "$ref"
i tried:
class Foo(object):
    def _ref(o):

setattr(Foo, "$ref", Foo._ref)

but when i do something like this in code:

class B(object):
b = Bar
f = Foo.getattr("$ref")

if get the following error
unbound method _ref() must be called with Foo instance as first
argument (got Bar instance instead)

I managed to impelemnt it with a unbound, "normal" _ref() function,
but i want one _ref function per
class, because they are a little bit different for each class.

One aproach which i have not tried is:
setattr(Foo, "$ref", Foo._ref.im_func).
maybe this works, but i think this is not a clean aproach.
Any ideas?

More information about the Python-list mailing list