Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
unexpected behaviour playing with dynamic methods
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Marc Aymerich  
View profile  
 More options Feb 23, 6:52 am
Newsgroups: comp.lang.python
From: Marc Aymerich <glicer...@gmail.com>
Date: Thu, 23 Feb 2012 03:52:27 -0800 (PST)
Local: Thurs, Feb 23 2012 6:52 am
Subject: unexpected behaviour playing with dynamic methods
Hi,

I'm playing a bit with python dynamic methods and I came up with a
scenario that I don't understant. Considering the follow code:

# Declare a dummy class
class A(object):
    pass

# generate a dynamic method and insert it to A class
for name in ['a', 'b', 'c']:
    if name == 'b':
        @property
        def get_name(self):
            return name
        A.name = get_name

a_instance = A()
a_instance.name

# So far I exptect that a_instance.name  returns 'b', since it has
been created when name == 'b', but this is what actually returns:

>>> a_instance.name

'c'

just the last 'name' value.
What can I do in order to generate a method like this but that returns
'b' ? What is wrong in my understanding of this pice of code?

Thanks !!!
marc


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Feb 23, 8:05 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Thu, 23 Feb 2012 14:05:24 +0100
Local: Thurs, Feb 23 2012 8:05 am
Subject: Re: unexpected behaviour playing with dynamic methods

Look at the method again:

>         def get_name(self):
>             return name

It returns the global variable name. Why would you expect to see a
historical value of that variable?
If you want to capture the value of name at the time when get_name() is
defined you have several options:

- The default argument trick:

def get_name(self, name=name):
    return name

- A closure:

def make_get_name(name):
    def get_name(self):
        return name
    return get_name
A.name = property(make_get_name(name))

- functools.partial()

def get_name(self, name):
    return name
A.name = property(partial(get_name, name=name))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marc Aymerich  
View profile  
 More options Feb 23, 8:44 am
Newsgroups: comp.lang.python
From: Marc Aymerich <glicer...@gmail.com>
Date: Thu, 23 Feb 2012 05:44:46 -0800 (PST)
Local: Thurs, Feb 23 2012 8:44 am
Subject: Re: unexpected behaviour playing with dynamic methods
On Feb 23, 2:05 pm, Peter Otten <__pete...@web.de> wrote:

hehe, yeah, after sending my email I realized that it was obvious, but
the true is I came up with this problem in a more complex situation
involving lot's of imports and so on.. so the question: wtf is going
on?? came up to me at this time :P

Wow Peter, I was expecting one solution and you give me 3 amazing
solutions!! :) Thanks a lot!

btw I always wondered for what functools.partial can be useful, now I
know an example :)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »