I try to understand the OOP and sketched a class that takes two numbers, then it must multiply them, then divide and at the end of the results of these two methods display

class Numbers_Class: def __init__(self, value1, value2): self.value1 = value1 self.value2 = value2 def multiply(self): self.value1*self.value2 def devision(self): self.value1/self.value2 def get_list(self): return [self.value1, self.value2] num_class = Numbers_Class(10, 5) print(num_class.get_list()) 

As a result, he prints [10, 5], but I want to get [50, 2]. How can I do it?

The essence of all these actions, I want to write a class in which there will be a bunch of methods, I will transfer data to it during initialization and after that I would like to call one get_list () method a sheet with the data in which there will be grouped information from each method. How to make methods run in class? So that I, in a miracle machine, poured milk roughly and put in sugar, and at the output received ice cream. without causing intermediate methods, did not press the button, freeze milk, mix sugar with milk and so on, and so that the class itself calls them.

  • He clarified his question - shatoidil

2 answers 2

 class Numbers_Class: def __init__(self, value1, value2): self.value1 = value1 self.value2 = value2 def get_list(self): return [self.value1*self.value2, int(self.value1/self.value2)] num_class = Numbers_Class(10, 5) print(num_class.get_list()) 

Given your last comment:

 class Numbers_Class: def __init__(self, value1, value2): self.value1 = value1 self.value2 = value2 def multiply(self): return self.value1*self.value2 def devision(self): return self.value1/self.value2 def get_list(self): return [self.multiply(), self.devision()] 
  • The essence of my question is, how do I call methods after class initialization? That is, I write like this num_class = Numbers_Class (10, 5) and then just like that print (num_class.get_list ()) and all methods are executed inside the class, in short the division and multiplication method starts after class initialization and in the end I take away the data get_list () - shatoidil
  • instead of int(a/b) , the author probably wanted a // b . - jfs
  1. Class methods are not executed by themselves. It’s not enough to write them, you also need to start it after the class instance is created.

For example:

 num_class = Numbers_Class(10, 5) num_class.multiply() num_class.devision() print(num_class.get_list()) 
  1. In addition to this, the multiply and devision methods are incorrectly written. They make calculations, but the result of these calculations is not used at all and does not affect anything.

The result needs to be written somewhere. For example:

 self.value1 = self.value1 * self.value2 
  1. However, even if you rewrite your methods in this way, you will not get exactly what you want. Because after the execution of the multiply () method, the value of value1 already changes and in the subsequent execution of the devision () method, the new value of 50 will be used.

Here we need to revise the logic. Alternatively, you can use a separate pair of variables to store the original values, and another pair for the current ones.

UPD: Based on your comment, you can do this:

 class Numbers_Class: def __init__(self, param1, param2): self.value1 = param1 * param2 self.value2 = param1 / param2 def get_list(self): return [self.value1, self.value2] 
  • It is impossible to make it so that at the entrance there are two numbers, and after initializing the class, I transferred two numbers to it and made just get_list and got a list with the numbers multiplied and multiplied. I guess I do not correctly formulate the question. Is it possible for a class to become one big method, that is, for division and multiplication methods to be performed inside it? - shatoidil
  • Then you can do without unnecessary methods, and all the calculations done in init. Now I will add my answer code. - Xander