Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of classmethod in this code?

Tags:

python

In django.utils.tree.py:

def _new_instance(cls, children=None, connector=None, negated=False):     obj = Node(children, connector, negated)     obj.__class__ = cls     return obj _new_instance = classmethod(_new_instance) 

I don't know what classmethod does in this code sample. Can someone explain what it does and how to use it?

like image 203
zjm1126 Avatar asked Dec 23 '09 02:12

zjm1126


People also ask

Where is Classmethod Python used?

In Python, the @classmethod decorator is used to declare a method in the class as a class method that can be called using ClassName. MethodName() . The class method can also be called using an object of the class. The @classmethod is an alternative of the classmethod() function.

What is advantage of Classmethod in Python?

It makes it clearer that the method doesn't use any state from the instance, usually named self . Also it means you can test it on the class without creating an instance.

When should I use Classmethod Python?

You can use class methods for any methods that are not bound to a specific instance but the class. In practice, you often use class methods for methods that create an instance of the class. When a method creates an instance of the class and returns it, the method is called a factory method.

What is the purpose of Classmethod and Staticmethod in Python?

The difference between the Class method and the static method is: A class method takes cls as the first parameter while a static method needs no specific parameters. A class method can access or modify the class state while a static method can't access or modify it.


2 Answers

classmethod is a decorator, wrapping a function, and you can call the resulting object on a class or (equivalently) an instance thereof:

>>> class x(object): ...   def c1(*args): print 'c1', args ...   c1 = classmethod(c1) ...   @classmethod ...   def c2(*args): print 'c2', args ...  >>> inst = x() >>> x.c1() c1 (<class '__main__.x'>,) >>> x.c2() c2 (<class '__main__.x'>,) >>> inst.c1() c1 (<class '__main__.x'>,) >>> inst.c2() c2 (<class '__main__.x'>,) 

As you see, whether you define it directly or with decorator syntax, and whether you call it on the class or the instance, the classmethod always receives the class as its first argument.

One of the main uses of classmethod is to define alternative constructors:

>>> class y(object): ...   def __init__(self, astring): ...     self.s = astring ...   @classmethod ...   def fromlist(cls, alist): ...     x = cls('') ...     x.s = ','.join(str(s) for s in alist) ...     return x ...   def __repr__(self): ...     return 'y(%r)' % self.s ... >>> y1 = y('xx') >>> y1 y('xx') >>> y2 = y.fromlist(range(3)) >>> y2 y('0,1,2') 

Now if you subclass y, the classmethod keeps working, e.g.:

>>> class k(y): ...   def __repr__(self): ...     return 'k(%r)' % self.s.upper() ... >>> k1 = k.fromlist(['za','bu']) >>> k1 k('ZA,BU') 
like image 121
Alex Martelli Avatar answered Sep 25 '22 22:09

Alex Martelli


It makes it possible to call the method on the class instead of an object:

class MyClass(object):     def _new_instance(cls, blah):         pass     _new_instance = classmethod(_new_instance)  MyClass._new_instance("blah") 
like image 39
Ned Batchelder Avatar answered Sep 23 '22 22:09

Ned Batchelder