2013-08-06 217 views
2

我想从类中调用另一种方法创建的某个方法。我将如何做到这一点?从同一个类中的另一个方法创建的某个类调用一个方法?

例如,我刚刚创建了一个名为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. 

我该如何添加第二个数字来减去返回的差值?有没有办法让我通过differenceadd

+0

'self.subtract()+ second_number' –

回答

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

如果我明白你的问题来,就更加编辑... –

+1

,回答我的问题。您的评论不清楚。它的字面意思是:如果我理解了你的问题(你所做的),然后再编辑它......看起来你会想要相反的。 – goldisfine

+1

因为你的问题包含评论'#代码添加两个数字,x和y ...' –

相关问题