Class methods are generally used to provide standardized processing; instance methods are generally used to provide personalized processing (binding to specific instances).
There should be no such thing as advantages and disadvantages.
If a method has the same input parameters, the output will be affected by the instance variables of different object instances. Generally, it should be written as an instance method.
If the input parameters are the same but are not affected by instance variables (static does not count as instance variables), you can write a class method and give it to the class to call. For example
You can write this kind of ghost as an instance method, but not only will one be instantiated/released every time it is called, it will also affect code reading.
Class methods are more common in singleton mode and factory mode. This class in the singleton mode program has only one instance; the factory mode produces different instances and returns them based on the parameters.
There is no distinction between the methods, they are just for convenience of use. If you have to distinguish, the following should be considered
Disadvantages of class methods: In class methods, self represents a class, not a class object, so in a class method you cannot use self. to call attributes, nor can you call instance methods of this class. Advantages of class methods: The call is simple, no need to create objects
Class methods are generally used to provide standardized processing; instance methods are generally used to provide personalized processing (binding to specific instances).
There should be no such thing as advantages and disadvantages.
If a method has the same input parameters, the output will be affected by the instance variables of different object instances. Generally, it should be written as an instance method.
If the input parameters are the same but are not affected by instance variables (static does not count as instance variables), you can write a class method and give it to the class to call. For example
You can write this kind of ghost as an instance method, but not only will one be instantiated/released every time it is called, it will also affect code reading.
Class methods are more common in singleton mode and factory mode. This class in the singleton mode program has only one instance; the factory mode produces different instances and returns them based on the parameters.
There is no distinction between the methods, they are just for convenience of use. If you have to distinguish, the following should be considered
Disadvantages of class methods: In class methods, self represents a class, not a class object, so in a class method you cannot use self. to call attributes, nor can you call instance methods of this class.
Advantages of class methods: The call is simple, no need to create objects