1. Attribute of class and attribute of class object
class Parent(object): a = 'a' b = 1 def __init__(self): self.a = 'A' self.b = 1.1 def print_info(self): print('a = %s , b = %s' % (self.a, self.b)) @staticmethod def static_test(self): print 'a static func.' @classmethod def class_test(self): print 'a calss func.' # class Son(Parent): p = Parent() print(Parent.__dict__) print(p.__dict__)
The operation results are as follows:
{'__module__': '__main__', 'a': 'a', 'b': 1, '__init__': <function Parent.__init__ at 0x0000000002168950>, 'print_info': <function Parent.print_info at 0x00000000021689D8>, 'static_test': <staticmethod object at 0x00000000021677B8>, 'class_test': <classmethod object at 0x0000000002167DD8>, '__dict__': <attribute '__dict__' of 'Parent' objects>, '__weakref__': <attribute '__weakref__' of 'Parent' objects>, '__doc__': None}
{'a': 'A', 'b': 1.1}
It can be seen that the static function, class function, ordinary function, global variable and some built-in properties of a class are all placed in the class "dict".
Some things of self.xxx are stored in the object's dict
2. Having inheritance relationship
The child class has its own dict, and the parent class has its own dict. The global variables and functions of the child class are placed in the child class's dict, and the parent class's are placed in the parent class's dict.
class Parent(object): a = 'a' b = 1 def __init__(self): self.a = 'A' self.b = 1.1 class Son(Parent): a = 'b' b = 2 def __init__(self): self.b = 2.2 self.a = 'B' p = Parent() print(Parent.__dict__) print(p.__dict__) s = Son() print(Son.__dict__) print(s.__dict__)
Run the above code and enter the following results:
{'__module__': '__main__', 'a': 'a', 'b': 1, '__init__': <function Parent.__init__ at 0x0000000002278950>, '__dict__': <attribute '__dict__' of 'Parent' objects>, '__weakref__': <attribute '__weakref__' of 'Parent' objects>, '__doc__': None} {'a': 'A', 'b': 1.1} {'__module__': '__main__', 'a': 'b', 'b': 2, '__init__': <function Son.__init__ at 0x00000000022789D8>, '__doc__': None} {'b': 2.2, 'a': 'B'}
Look at the built-in data type again, class = = type, but the built-in data does not have the attribute
num = 3 ll = [] dd = {}
Operation result:
Traceback (most recent call last): File "D:/python_studay/day8/__dict__Property details.py", line 31, in <module> num.__dict__ AttributeError: 'int' object has no attribute '__dict__'
Conclusion:
1. The built-in data type has no "dict" attribute.
2. The class' dict 'stores the data in the namespace of the class, but the object' dict 'attribute instantiated by the class only stores the data in the namespace of the object. For example, self.xx
3. The class has inheritance relationship. The parent class's dict does not affect the child class's dict.__