[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

staticmethod and setattr

Michael.Lausch

3/15/2010 8:43:00 AM

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?




3 Answers

Steven D'Aprano

3/15/2010 10:41:00 AM

0

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




--
Steven

Michael.Lausch

3/15/2010 12:42:00 PM

0

On Mar 15, 11:40 am, Steven D'Aprano <st...@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.

Bruno Desthuilliers

3/15/2010 1:52:00 PM

0

Michael.Lausch a écrit :
(snip)


> 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?
>

It's an application of the descriptor protocol:

http://wiki.python.org/moin/FromFuncti...