Created
November 5, 2013 14:54
-
-
Save ghaering/7320226 to your computer and use it in GitHub Desktop.
Dynamic method creation does not work as excpected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> class API: | |
... def __init__(self): | |
... pass | |
... | |
>>> for i in range(10): | |
... name = "print_number_%i" % i | |
... def print_func(self): | |
... print "my result is", i | |
... setattr(API, name, print_func) | |
... | |
>>> api = API() | |
>>> api.print_number_3() | |
my result is 9 | |
>>> # why not 3?! | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment