从类中调用由同一类中不同方法创建的方法?

2024-04-20 07:23:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从一个类中调用一个方法,这个方法是由类中的另一个方法创建的。我该怎么做?你知道吗

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

如何在减法返回的差中再加一个数?有没有办法把difference传给add?你知道吗


Tags: theto方法selfaddnumberobjectinit