Python built-in class attribute
I think everything is an object, the object and meta-class objects, class objects are actually the same, I finally had proof, but only proves half, due yuan last class is the parent class type, he can block for object properties visit ended
__dict__: class attributes (including a dictionary, the data class attribute composition)
__doc__: Class documentation strings
__name__: class name
__module__: where the class definition module (the full name of the class is the ' main .className', if introduced in a class module mymod, then className .__ module__ equal mymod)
__base__: first class of the parent class
__bases__: All the elements constituting the parent class (a tuple contains all of the parent classes)
__class__: create objects of this class
__dict__
Plus objects .
can come out is the presence inside it.
laboratory
Example 1
It was found from the print, z object __dict__()
is empty, why he can print out things? It follows the property to find the following relation (requires knowledge of the meta-class)
class zx():
pass
z=zx()
print(z.__dict__)
print(z.__module__)
{}
__main__
Example 2
Code has two attributes are my comments, and why it? Because it finds type
are not found, you will get an error (can, zx,object,type
of __dict__
all printed out, and then in the console search)
class zx():
pass
print(zx)
print(zx.__dict__)
print(zx.__class__)
print(zx.__bases__)
print(object.__dict__)
print(type.__dict__)
print(zx.__name__)
print(zx.__doc__)
# print(zx.__slots__)
print(zx.__qualname__)
print(zx.__module__)
# print(zx.__annotations__)
print(zx.__mro__)
Then find the property should be divided into two layers, one layer is to locate the object (c3 algorithm based MRO), and the other layer is a lookup based layer (i.e. metaclass layer)
#查找顺序:
#1、先对象层:OldoyTeacher->Foo->Bar->object
#2、然后元类层:Mymeta->type
to sum up
1. Call yourself __dict__
inside the method, then direct object calls on the line, and will not pass a default object parameter, if no use is necessary to use a lookup relationship
2. Object
start looking for their own dict
And then look for from their own inheritance
Start looking metaclass
Finally, look for the inheritance yuan class
Metaclass attribute precedence over class inheritance yuan
class cc():
name="das"
class zx(cc):
name="20"
z=zx()
print(z.name)
20
class Mytype(type):
name=20
class zx(metaclass=Mytype):#zx=Mytype()
pass
print(zx.__doc__)
None #type的__doc__不为None
Inheritance __bases__
only object there
Their inheritance precedence over property metaclasses
class cc():
name=20
class Mytype(type):
name=10
class zx(cc,metaclass=Mytype):
pass
#相当于zx=Mytype(...)
print(zx.name)
20
Objects can not prove this, not the object __bases__
methods, object did not, but there type