Aug-26-2018, 07:46 PM
(This post was last modified: Aug-26-2018, 07:46 PM by Gribouillis.)
The most important call is
Now look at this method at line 11. It looks in a global dictionary
if we have already stored a list of property names
Last subtlety:
add_property_names(A)
if you look at what it does in line 18, it adds a new classmethod named property_names
to class A. Everything happens as if we had writtenclass A: @classmethod def property_names(cls): ...It means that we can now call
A.property_names()
or A().property_names()
or even C.property_names()
because C is a subclass of A.Now look at this method at line 11. It looks in a global dictionary
_register
if we have already stored a list of property names
_register[A]
for class A. If not it computes this list by calling _get_names(A)
. This way the list of properties is only computed once for each class.Last subtlety:
add_property_names(cls)
returns cls
. This allows us to use it as a class decorator and write@add_property_names class A: passso that we don't need to call explicitly
add_property_names
after the definition of class A.