In the example below, How do I call function 1
in function 2
. I have tried it just like in the example, but does not recognise para
in function 2
and I have also tried replacing it with self.para
which gets a undefined error..what is the right way to call function 1
? I can't add another argument in function2(self)
.
class Example:
#def...
def function1(self, para):
#bla bla
def function2(self):
xxx = self.function1(para)
# how do I call a function such as this
class Example:
#def...
def function1(self, para):
#bla bla
def function2(self):
para = 12 # make sure the variable you pass to function1 is defined
xxx = self.function1(para)
# how do I call a function such as this
You can only reference self.para
if class Example has been instantiated and has an attribute called para
.
If class Example
has attributes that are static, you can reference those through an instance (self) or through the class.
Instantiating and referencing self:
class Example:
def __init__(self): # constructor method
self.value = 12 # instance attribute
def func1(self, parameter):
# do stuff
return ...
def func2(self):
x = func1(self.value)
or from static context:
class Example:
value = 12 # class attribute
def func1(self, parameter):
# do stuff
return ...
def func2(self):
x = func1(Example.value)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.