简体   繁体   English

如何在相同 class 的另一个 function 中使用 function 的值

[英]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.

相关问题 如何在openerp的同一类的另一个函数中使用一个函数变量? - How to use one function variable in another function of same class in openerp? 如何在另一个 function 中使用 class - How to use a class in another function 返回具有多个输入参数的函数的值以在同一类中具有多个参数的另一个函数中使用? - Returning the value of a function with multiple input parameters to use in another function with multiple parameters inside the same Class? 如何将类中函数的返回值传递给同一个类中的另一个函数? - How do I pass a return value in a function in a class to another function within the same class? 如何在同一个 function 中使用 function 的值 - How to use the value of a function within the same function 如何在另一个 function 中使用一个值 function - How to use a value in one function in another function 如何使用另一个类的函数类? - How to use function class from another class? 如何在同一类的另一个函数中使用一个函数中的一个对象 - How do I use one object from one function in another function in the same class 如何在同一类的另一个函数中定义的函数中访问字典 - How to access a dictionary in a function that is defined in another function in the same class 在同一类的另一个函数中获取一个函数的结果 - Getting result of a function in another function of the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM