Created
October 2, 2010 22:38
-
-
Save littlefolk/608064 to your computer and use it in GitHub Desktop.
Python's Inner function getattr questions?
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 Foo: | |
def getattr(self, arg): | |
def x(): | |
return "good" | |
return getattr(self, arg, None) | |
def x(self): | |
return "bad" | |
f = Foo() | |
print f.getattr("x")() # => bad | |
class Bar: | |
def getattr(self, arg): | |
def x(): | |
return "good" | |
if arg in locals(): | |
return locals()[arg] | |
else: | |
raise | |
def x(self): | |
return "bad" | |
b = Bar() | |
print b.getattr("x")() # => good |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment