简体   繁体   中英

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.

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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