+ 13

What's the difference between property and classmethod?

i realized they are both kind of decorators

11th Jun 2018, 5:47 PM
misgav yosef
misgav yosef - avatar
1 Resposta
+ 10
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
12th Jun 2018, 1:03 AM
1_coder