Skip to content

Instantly share code, notes, and snippets.

@littlefolk
Created October 2, 2010 22:38
Show Gist options
  • Save littlefolk/608064 to your computer and use it in GitHub Desktop.
Save littlefolk/608064 to your computer and use it in GitHub Desktop.
Python's Inner function getattr questions?
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