Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > staticmethod and setattr

Reply
Thread Tools

staticmethod and setattr

 
 
Michael.Lausch
Guest
Posts: n/a
 
      03-15-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?




 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-15-2010
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
 
Reply With Quote
 
 
 
 
Michael.Lausch
Guest
Posts: n/a
 
      03-15-2010
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.

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      03-15-2010
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/FromFunctionToMethod
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: staticmethod and setattr Andreas Löscher Python 1 03-15-2010 06:25 PM
staticmethod and namespaces darnzen Python 0 02-26-2010 04:54 AM
staticmethod and classmethod C Gillespie Python 5 05-26-2005 09:46 PM
Re: __getitem__ and classmethod/staticmethod Andrew Bennetts Python 0 06-25-2004 06:00 PM
__getitem__ and classmethod/staticmethod Karl Chen Python 0 06-25-2004 05:18 PM



Advertisments