Skip to content

Instantly share code, notes, and snippets.

@ghaering
Created November 5, 2013 14:54
Show Gist options
  • Save ghaering/7320226 to your computer and use it in GitHub Desktop.
Save ghaering/7320226 to your computer and use it in GitHub Desktop.
Dynamic method creation does not work as excpected
>>> 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