We have familiarized ourselves with the basic concepts of objects and classes. We will expand further so that we can use objects and classes in a practical way.
Additional information about the calling class
previous sessionIt is mentioned that when defining a method, it must have the parameter self. This parameter represents an object. The object has all the properties of the class, so we can call the class properties through self.
class Human(object):
laugh = 'hahahaha'
def show_laugh(self):
def laugh_100th(self):
for i in range(100):
self.show_laugh()
li_lei = Human()
li_lei.laugh_100th()
Here there is a class attribute LAUGH. the value of this attribute is called in the method show_laugh(), by,...
Other methods can also be called in the same way. Method show_laugh(), called in method laugh_100th().
It is possible to modify class attribute values through objects. But this is dangerous. Class attributes are shared by all objects of the same class and its subclasses. A change in the value of a class attribute affects all objects.
__init__() method
__init__() is a special method.Python has some special methods.Python treats them specially. Special methods are characterized by two underscores before and after the name.
If you define the method __init__() in your class, Python will automatically call this method when you create an object. This process is also called initialization.
class happyBird(Bird):
def __init__(self,more_words):
print 'We are happy birds.',more_words
summer = happyBird('Happy,Happy!')
The Bird class is inherited here, and its definition is described in the previous lecture.
On-screen printing:
We are happy ,Happy!
We see that the __init__() method is called automatically, even though we just created the summer object. The last line of the statement (summer = happyBird...) creates the object first and then executes it:
summer.__init__(more_words)
'Happy,Happy!' is passed as argument to __init__() with more_words
Nature of the object
We talked about many properties, but these properties are attributes of the class. All objects belonging to the class will share these properties. For example, all birds have feathers and all chickens can't fly.
In some cases, we define properties of an object that are used to record special information about that object. For example, the class Human. Gender is a property of a particular person; not all humans are male or all female. The value of this property varies with the object. Li Lei is an object of a human being, and his gender is male; Han Mei Mei is also an object of a human being, and her gender is female.
When defining a method of a class, a self argument must be passed. This argument refers to an object of the class. We can manipulate self to modify the properties of an object. For example, if we create a new object with the class, i.e., li_lei in the following example, then li_lei is represented by self. We add properties to li_lei, such as the gender of the object, by assigning it to self, which is passed to the methods. Inside the methods, the properties of the object can be queried or modified by reference.
In this way, in addition to the class attributes, each object is given its own distinctive properties, thus being able to describe diverse worlds.
class Human(object):
def __init__(self, input_gender):
= input_gender
def printGender(self):
li_lei = Human('male') # Here, 'male' is passed as an argument to the input_gender variable of the __init__() method.
print li_lei.gender
li_lei.printGender()
In the initialization, the parameter input_gender, is assigned to the nature of the object, ie.
li_lei has the object property gender. gender is not a class property. after Python creates the object li_lei, it uses the object property li_lei.gender to store information specific to the object li_lei.
Properties of objects can also be called by other methods in a similar way to calls to class properties, as in the printGender() method.
summarize
Calling class attributes via self
__init__(): executed automatically when creating an object
Difference between class attributes and object properties