Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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中的内部方法_Python - Fatal编程技术网

从另一个方法访问python中的内部方法

从另一个方法访问python中的内部方法,python,Python,我试图从另一个方法访问python中的内部方法,但这样做会给我“AttributeError:‘function’对象没有属性‘b’” 我的设想是: class Foo: def first_method(self): something def test(self): print 'Hi' def second_method(self): a = self.test() 行a=self.test()。如果您

我试图从另一个方法访问python中的内部方法,但这样做会给我“AttributeError:‘function’对象没有属性‘b’”

我的设想是:

class Foo:
    def first_method(self):
        something
        def test(self):
           print 'Hi'

    def second_method(self):
       a = self.test()

a=self.test()。如果您想在其他功能中访问它,您必须在某处保留对它的引用。类似于以下的方法将起作用:

>>> class Foo:
...     def first_method(self):
...         def test():
...            print 'Hi'
...         self.test = test
...     def second_method(self):
...         self.test()
... 
>>> f = Foo()
>>> f.second_method()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in second_method
AttributeError: Foo instance has no attribute 'test'
>>> f.first_method()
>>> f.second_method()
Hi
>>类Foo:
...     def第一法(自):
...         def test():
...            打印“嗨”
...         自我测试
...     def第二法(自):
...         self.test()
... 
>>>f=Foo()
>>>f.第二种方法()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第7行,第二种方法
AttributeError:Foo实例没有属性“test”
>>>f.第一种方法()
>>>f.第二种方法()
你好

请注意,代码中的问题有一些更改。例如,函数
test
不接受任何参数。还要注意的是,在调用
第一个\u方法之前必须先调用
第二个\u方法
函数
测试
仅在
第一个\u方法
的本地范围内可用。如果您想在其他功能中访问它,您必须在某处保留对它的引用。类似于以下的方法将起作用:

>>> class Foo:
...     def first_method(self):
...         def test():
...            print 'Hi'
...         self.test = test
...     def second_method(self):
...         self.test()
... 
>>> f = Foo()
>>> f.second_method()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in second_method
AttributeError: Foo instance has no attribute 'test'
>>> f.first_method()
>>> f.second_method()
Hi
>>类Foo:
...     def第一法(自):
...         def test():
...            打印“嗨”
...         自我测试
...     def第二法(自):
...         self.test()
... 
>>>f=Foo()
>>>f.第二种方法()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第7行,第二种方法
AttributeError:Foo实例没有属性“test”
>>>f.第一种方法()
>>>f.第二种方法()
你好

请注意,代码中的问题有一些更改。例如,函数
test
不接受任何参数。还要注意的是,如果您希望
test()
成为
Foo
的一个正常方法,那么必须在
second\u方法之前调用
first\u方法
,是否有任何理由不将
test()
定义为
Foo
的正常方法,而是定义为
first\u方法()
中的一个本地函数?您不能这样做。你为什么要这样做?如果你想
test()
成为
Foo
的一个普通方法,你有什么理由不把
test()
定义为
Foo
的一个普通方法,而是定义为
first\u method()
中的一个本地函数吗?你不能。为什么要这样做?请注意,分配给实例属性的函数不是方法;调用时,它们没有访问实例的权限,也就是说,它们不会通过
self
@SvenMarnach,它们不会通过
self
,但在这段代码中,这并不意味着它们没有访问它的权限(由于闭包语义)。另外,在其他情况下,通过执行
self.test=types.MethodType(test,self)
@lvc:完全同意,这个问题是可以解决的。我只是认为这是一个需要注意的问题,特别是因为OP为本地函数
test()
使用了显式的
self
参数;调用时,它们没有访问实例的权限,也就是说,它们不会通过
self
@SvenMarnach,它们不会通过
self
,但在这段代码中,这并不意味着它们没有访问它的权限(由于闭包语义)。另外,在其他情况下,通过执行
self.test=types.MethodType(test,self)
@lvc:完全同意,这个问题是可以解决的。我只是认为这是一个需要注意的问题,特别是因为OP为本地函数
test()
使用了显式的
self
参数。