When I call func1 from func2, I get an error. Why is this occurring and how to access the returned dictionary d in func1 from func2?
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
Konrad Rudolph
550k142 gold badges968 silver badges1.3k bronze badges
2 Answers 2
func1 and func2 are instance methods of ABC objects. You need to call them from the instance of such object.
In your case, one such instance is self, first parameter of both functions.
Thus, self.func1() will work.
answered Jan 31, 2022 at 22:31
matszwecja
8,2472 gold badges13 silver badges22 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Use self.func1() to call the function inside your class.
answered Jan 31, 2022 at 22:31
Nuno Carvalho
3021 silver badge5 bronze badges
Comments
lang-py
class ABC:parthensis are not needed hered = self.func1()calling a method and setting returned value tod(although you could use a completely different name here, e.g.result = self.func1())