What's the difference between property and classmethod? | Sololearn: Learn to code for FREE!

+11

What's the difference between property and classmethod?

i realized they are both kind of decorators

6/11/2018 5:47:39 PM

misgav yosef

1 Answer

New Answer

+4

A property allows you to define an attribute of your object. It works on instances. A classmethod operates on the class independent of any instance. Examples are helpful: class A: _name = 'foo' @classmethod def print_name(cls): print(cls._name) @property def name(self): return self._name # print the default class name A.print_name() #foo # create an instance, change its name, print it a = A() a._name = 'bar' print(a._name) #bar # classmethod does not see instance data a.print_name() #foo # class name is unchanged by instances A.print_name() #foo