A class is usually an object type. Class instances are specific objects of this type. A classic example is the Animal class. a cat will be an instance of Animal . (I assume that you mean those that belong to the instance, not the class object itself) should be used for instance attributes. In this case, for example, colour may be an attribute of a class that will be set as cat.colour = "white" or bear.colour = "brown" . Arguments should be used where the value may come from some source outside the class. If the Animal class has a sleep method, it may need to know the duration of sleep and posture in which the animal sleeps. duration will be the argument of the method, since it is not related to the animal, but posture will be a class variable, since it is determined by the animal.
In python, a class is usually used to group together a set of functions and variables that share state. Continuing the above example, a particular animal has a state that is shared between its methods and is determined by its attributes. If your class is just a group of functions that are in no way dependent on the state of the class, then they can just as easily be separate functions.
If __init__ used to calculate the final result (which should be stored in the class attribute, since __init__ cannot return the result), then you can also use the function. However, the general scheme is to do a lot of processing in __init__ several other, sometimes private, class methods. The reason for this is that large complex functions are often easier to test if they are broken down into smaller and more precise tasks, each of which can then be tested individually. However, this is usually done only when the class is needed anyway.
One approach to an entire business is to start by deciding what kind of functionality you need. When you have a group of functions or variables that all act or apply to the same object, then it's time to move them to the class. Remember that object-oriented programming (OOP) is a design method that is suitable for some tasks, but is not inherently a super-functional programming (in fact, some programmers will argue about the opposite!), Therefore there is no need to use classes, if not really necessary.
source share