Python 3.x 类中的嵌套函数,Python
我试图在类中嵌套函数。这是我的外套Python 3.x 类中的嵌套函数,Python,python-3.x,function,class,nested,Python 3.x,Function,Class,Nested,我试图在类中嵌套函数。这是我的外套 class big(): def __init__(self): self.mas = "hello" def update(self): def output(self): print(self.mas) self.output() thing = big() thing.update() 但是,当它运行时,我得到一个错误,输出未定义。如何在更新函数中
class big():
def __init__(self):
self.mas = "hello"
def update(self):
def output(self):
print(self.mas)
self.output()
thing = big()
thing.update()
但是,当它运行时,我得到一个错误,输出未定义。如何在更新函数中运行输出函数?只需将其称为
output()
,而不使用self
。按照您定义它的方式,它基本上是update
方法中的局部变量,而不是类的属性
class big():
def __init__(self):
self.mas = "hello"
def update(self):
def output():
print(self.mas)
output()
thing = big()
thing.update()
只需将其称为
output()
,而不使用self
。按照您定义它的方式,它基本上是update
方法中的局部变量,而不是类的属性
class big():
def __init__(self):
self.mas = "hello"
def update(self):
def output():
print(self.mas)
output()
thing = big()
thing.update()
您可能不应该将
self
作为arg发送到output
,但是如果这样做,您应该在调用时传递它。目前,它引发了TypeError:output()缺少1个必需的位置参数:“self”
@ReblochonMasque我的错误,修复了它您可能不应该将self
作为参数传递给output
,但是如果这样做,您应该通过调用传递它。目前,它引发了TypeError:output()缺少1个必需的位置参数:“self”
@ReblochonMasque我的错误,修复了它