staticmethod and setattr

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


Hi,

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):
    @staticmethod
    def _ref(o):
         pass

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

but when i do something like this in code:

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

if get the following error
ypeError
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