繁体   English   中英

从同一类中的不同方法创建的对象上调用类中的方法?

[英]Call a method from a class on something created by a different method in the same class?

我想从类中的方法调用类中的另一个方法创建的东西。 我该怎么做?

例如,我刚刚创建了一个名为call me的类,并想在add方法中使用subtract方法的结果。

class call_me(object):

   def __init__(self, x, y):
         self.x = 5
         self.y = 10

   def subtract(self):
        difference = self.y - self.x
        return difference

   def add(self.subtract,second_number):
        # code to add the difference returned by subtract to the second number.

如何将第二个数字加到减法返回的差值中? 有什么办法可以add difference吗?

def add(self, second_number):
    difference = self.substract()
    return difference + second_number

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM