Created
March 31, 2012 17:18
-
-
Save TheFeshy/2266871 to your computer and use it in GitHub Desktop.
Dynamically add "convenience" functions from a dictionary of values
This file contains hidden or 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 A(object): | |
items = {"one":1, | |
"two":2} | |
def __init__(self): | |
self.item = 0 | |
for key in A.items: | |
f = lambda self,k=key:(self.__setattr__("item", self.items[k]),self)[1] | |
f.__name__=key | |
setattr(A,key,f) | |
a = A() | |
print a.one() #Prints the a object | |
print a.item #prints "1" | |
print a.two() #prints the a object | |
print a.item #prints "2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment