staticmethod and setattr

Discussion in 'Python' started by Michael.Lausch, Mar 15, 2010.

  1. 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?
     
    Michael.Lausch, Mar 15, 2010
    #1
    1. Advertising

  2. 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
     
    Steven D'Aprano, Mar 15, 2010
    #2
    1. Advertising

  3. 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.
     
    Michael.Lausch, Mar 15, 2010
    #3
  4. 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
     
    Bruno Desthuilliers, Mar 15, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Bennetts

    Re: __getitem__ and classmethod/staticmethod

    Andrew Bennetts, Jun 25, 2004, in forum: Python
    Replies:
    0
    Views:
    495
    Andrew Bennetts
    Jun 25, 2004
  2. Karl Chen
    Replies:
    0
    Views:
    348
    Karl Chen
    Jun 25, 2004
  3. C Gillespie

    staticmethod and classmethod

    C Gillespie, May 24, 2005, in forum: Python
    Replies:
    5
    Views:
    5,780
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    May 26, 2005
  4. darnzen

    staticmethod and namespaces

    darnzen, Feb 26, 2010, in forum: Python
    Replies:
    0
    Views:
    272
    darnzen
    Feb 26, 2010
  5. Andreas Löscher

    Re: staticmethod and setattr

    Andreas Löscher, Mar 15, 2010, in forum: Python
    Replies:
    1
    Views:
    820
    Jean-Michel Pichavant
    Mar 15, 2010
Loading...

Share This Page