我如何在方法内访问qux
?我真的需要在foo()
的主体内重新定义它吗,还是有一种从self
导入它的方法?
class Baz(object):
qux = lambda x : x + '_quux'
def foo(self, bar=qux('fred')):
print bar
print qux('waldo')
# NameError: global name 'qux' is not defined
print Baz.qux('waldo')
# TypeError: unbound method <lambda>() must be called with Baz instance as first argument (got str instance instead)
print Baz.qux(self, 'waldo')
# TypeError: <lambda>() takes exactly 1 argument (2 given)
print self.qux('waldo')
# TypeError: <lambda>() takes exactly 1 argument (2 given)
from os.path import expanduser;
def foo(self, bar=expanduser('~/default'))
,你能推荐一种优雅的方法来实现吗? - wimbar=None
并检查bar is None
。 - Ignacio Vazquez-Abrams