简体   繁体   English

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

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

I would like to call a method from a class on something created by another method in the class. 我想从类中的方法调用类中的另一个方法创建的东西。 How would I do this? 我该怎么做?

For example, I've just created a class called call me, and would like to use the result of the subtract method in the add method. 例如,我刚刚创建了一个名为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.

How would I add a second number to the difference returned by subtract? 如何将第二个数字加到减法返回的差值中? Is there a way I could pass difference to add ? 有什么办法可以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