Python object-oriented-class attributes and instance attributes
Python object-oriented-class attributes and instance attributes
I. instance attributes
Python is a dynamic language. You can bind attributes to instances created based on classes.
>>> Class Student (object ):
... Def _ init _ (self, name ):
... Self. name = name # attributes required by each instance
...
>>> S = Student ('jack ')
>>> S. score = 90 # Any property bound
>>> S. name
'Jack'
>>> S. score
90
Ii. class attributes
Define attributes directly in the class. This attribute is a class attribute.Student
Class.
>>> Class Student (object ):
... Name = 'jack' # class attributes
...
>>> Student. name # obtain class attributes
'Jack'
>>> S = Student ()
>>> S. name # Get class attributes
'Jack'
>>> S. name = 'Mike '# Add instance attributes
>>> S. name # obtain instance attributes
'Mike'
>>> Student. name # obtain class attributes
'Jack'
When writing a program, do not use the same name for the instance attributes and class attributes, because the instance attributes with the same name will block the class attributes, but after you delete the instance attributes, use the same name to access the class attributes.