I know there are alternatives to call the functions without classmethod director? But what is the advantage apart from calling it with class argument?
Asked
Active
Viewed 53 times
0
-
Other OO languages have this construct, e.g. in Java and C++ these are called static methods. It's provides a means of organizing related code to the class that doesn't need an instance. You could implement them outside of the class but it might lack the cohesion you want. Advantages to what, a normal function or an instance method? – AChampion Sep 14 '15 at 05:40
-
I agree with differences with respect to static methods and calling class functions using class name. But when and why do we need it? I am looking for a real case scenario specially for class method. – jsk Sep 14 '15 at 05:50
-
1See the accepted answer of [this question](http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner) – Mp0int Sep 14 '15 at 06:42
1 Answers
0
Using the classmethod decorator It act's like an alternate constructor.
althought it'll still need to use the arguments needed for your __init__
you can do other stuff in it. for simplicity's sake. It's like another __init__
method for your class.

Zion
- 1,570
- 6
- 24
- 36
-
There is no requirement for a classmethod to return an instance of the class. – Ignacio Vazquez-Abrams Sep 14 '15 at 06:22
-
while true you don't need to instantiate the class to use the class method. Conceptually. It's an alternate constructor. what I meant by using the args of the `__init__` is if your class has an `__init__` and needs 3 arguments you'll also need those same number of arguments when you return the `class` in the `@classmethod`. hence an alternate constructor in the most basic sense – Zion Sep 14 '15 at 06:28