简体   繁体   English

从另一个 function 调用 function

[英]Calling function from another function

When I call func1 from func2, I get an error.当我从 func2 调用 func1 时,出现错误。 Why is this occurring and how to access the returned dictionary d in func1 from func2?为什么会发生这种情况以及如何从 func2 访问 func1 中返回的字典 d?

 func1() NameError: name 'func1' is not defined
class ABC():

    def func1(self, a):
         d = {}
         ###do something with a
         ###return ending dictionary d
         return d

    def func2(self):
         ###Retrieve returned dictionary d from func1 and use
         func1()
         d['key'] = value

func1 and func2 are instance methods of ABC objects. func1 和 func2 是 ABC 对象的实例方法。 You need to call them from the instance of such object.您需要从此类 object 的实例中调用它们。 In your case, one such instance is self , first parameter of both functions.在您的情况下,一个这样的实例是self ,两个函数的第一个参数。 Thus, self.func1() will work.因此, self.func1()将起作用。

Use self.func1() to call the function inside your class.使用self.func1()调用 class 中的 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM