Skip to content Skip to sidebar Skip to footer

Call A Parameter Of A Function In Another One

I have two function f1 and f2 inside of a class. class Ex(): def f1(self,a): ... def f2(self) print(a) ex = Ex() ex.f1(10) ex.f2() What I want is to get

Solution 1:

There are several ways this could be done. one way would be to make a "setter" in your class:

class Ex():
    a = None # deafult value
    def function_1(self,a):
        Ex.a = a
        ...
    def function_2(self)
        print(Ex.a)

eg:

>>> class foo:
    a = None
    def f(self, a):
        foo.a = a
    def b(self):
        print(foo.a)


>>> f = foo()
>>> f.f(12)
>>> f.b()
12
>>> 

However, what i suggest you do instead, is to pass the return value of function_1() to function_2():

class Ex():
    def function_1(self,a):
        ...
        ...
        return a
    def function_2(self, a)
        print(a)
...
...
ex = Ex()
a = ex.function_1()
ex.function_2(a)

Post a Comment for "Call A Parameter Of A Function In Another One"