Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 类中的嵌套函数,Python_Python 3.x_Function_Class_Nested - Fatal编程技术网

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我的错误,修复了它