staticmethod and setattr

Michael.Lausch mick.lausch at gmail.com
Mon Mar 15 13:42:01 CET 2010


On Mar 15, 11:40 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Mon, 15 Mar 2010 01:43:02 -0700, Michael.Lausch wrote:
> > 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)
>
> That doesn't work as expected:
>
> >>> Foo.__dict__['_ref'] is Foo.__dict__['$ref']
>
> False
>
> Try this instead:
>
> >>> setattr(Foo, "$ref", Foo.__dict__['_ref'])
> >>> Foo.__dict__['_ref'] is Foo.__dict__['$ref']
>
> True

Now I'm trying to understand why this is the case.
How is Foo.__dict__['_ref']  different from Foo._ref?
Shouldn't it return the same attribute?

And after further experiments i found out that a making
Foo._ref a classmethod does work with my first approach.




More information about the Python-list mailing list