[英]How to use value of a function in another function of same class
class test():
def abc():
a = 5;
b = 6;
c = 7;
return a,b,c
def defg():
print('inside defg second function')
a,b,c = abc()
return a,b,c
when I try to access it a = test(),a.abc()当我尝试访问它时 a = test(),a.abc()
TypeError: abc() takes 0 positional arguments but 1 was given TypeError: abc() 取 0 位置 arguments 但给出了 1
class Test():
def abc(self):
a = 5
b = 6
c = 7
return a,b,c
def defg(self):
print('inside defg second function')
a,b,c = self.abc()
return a,b,c
c = Test()
c.defg()
First of all, all class methods must have self
first parameter.首先,所有 class 方法都必须有self
的第一个参数。 Second, you need to use self.
其次,你需要使用self.
in one method if you want to call another method of your object.如果您想调用 object 的另一种方法,请使用一种方法。 And finally, it is better to call your classes from capital letter.最后,最好用大写字母来称呼你的课程。
Otherwise you cane use static methods in your class if your code don't work with instance variables:否则,如果您的代码不适用于实例变量,您可以在 class 中使用static 方法:
class Test():
@staticmethod
def abc():
a = 5
b = 6
c = 7
return a,b,c
@staticmethod
def defg():
print('inside defg second function')
a,b,c = Test.abc()
return a,b,c
c = Test()
c.defg()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.