Classes in Python
As the saying goes, things are clustered by classes, people are grouped by groups, and classes are a set of identical attributes. Next, we will discuss the relationship between human beings and human beings in Python.
First, we define a person's class as follows:
class People(object): '''Creating Human Beings''' def __init__(self,name,age,sex,blood_type): """People have the attributes of name, age, sex and blood group.""" self.name = name self.age = age self.sex = sex self.blood_type = blood_type def walk(self): '''Creating a Walking Method''' print("%s can walk!!!!" %self.name) def speak(self): """Create Speech Properties""" print("%s can speak!!" %self.name) def learn(self): '''Create learning attributes''' print("%s is learning." %self.name) p1 = People("wupeiqi",18,"female","0") p2 = People("Alex",8,"male","AB")
In the code above, we define a person's class, who has the attributes of name, age, sex, blood type, and so on, as well as the methods of speaking, learning and walking; and we have created two people, a man and a woman, "Wupeiqi" and "Alex". Next, we will improve these two people step by step.
We know that men have laryngeal knots and women have children. These two are the differences between men and women, and also their unique attributes. Then add this attribute to "wupeiqi" and "Alex". As follows:
class People(object): '''Creating Human Beings''' def __init__(self,name,age,sex,blood_type): """People have the attributes of name, age, sex and blood group.""" self.name = name self.age = age self.sex = sex self.blood_type = blood_type def walk(self): '''Creating a Walking Method''' print("%s can walk!!!!" %self.name) def speak(self): """Create Speech Properties""" print("%s can speak!!" %self.name) def learn(self): '''Create learning attributes''' print("%s is learning." %self.name) p1 = People("wupeiqi",18,"female","0") p2 = People("Alex",8,"male","AB") p1.birth = "Woman can give birth to a baby" #Adding Childbirth Function to Women p2.adam = "Man has Adam's apple" #Added Adhesive Attribute to Men
We know that both men and women have the function of learning. Some of us can speak English, others can speak Japanese, and all kinds of languages. These are attributes of individuals, such as "wupeiqi" can speak Japanese, and "Alex" can speak English. Let's realize that:
class People(object): '''Creating Human Beings''' def __init__(self,name,age,sex,blood_type): """People have the attributes of name, age, sex and blood group.""" self.name = name self.age = age self.sex = sex self.blood_type = blood_type def walk(self): '''Creating a Walking Method''' print("%s can walk!!!!" %self.name) def speak(self): """Create Speech Properties""" print("%s can speak!!" %self.name) def learn(self): '''Create learning attributes''' print("%s is learning." %self.name) p1 = People("wupeiqi",18,"female","0") p2 = People("Alex",8,"male","AB") p1.birth = "Woman can give birth to a baby" #Adding Childbirth Function to Women p2.adam = "Man has Adam's apple" #Added Adhesive Attribute to Men p1.language1 = "Japanese" p2.language2 = "English" print("{0} is can speak {1}".format(p1.name,p1.language1)) #Print the language wupeiqi speaks print("{0} is can speak {1}".format(p2.name,p2.language2)) #Print ALex Speaking Language print("%s is can speak %s" %(p1.name,p1.language2)) #See if wupeiqi can speak English print("%s is can speak %s" %(p2.name,p2.language1)) #See Alex Whether or not you can speak Japanese depends mainly on whether your language abilities can be crossed.
The results are as follows:
wupeiqi is can speak Japanese
Alex is can speak English
Traceback (most recent call last):
File "/home/zhuzhu/Day 7/Human beings.py", line 33, in <module>
print("%s is can speak %s" %(p1.name,p1.language2)) #See if wupeiqi can speak English
AttributeError: 'People' object has no attribute 'language2'
As can be seen from the above code, "wupeiqi" can speak Japanese, but "Alex" does not have the ability to "wupeiqi". This is like our human beings, you have the ability, others do not necessarily have, others do not necessarily have the ability, you also do not necessarily have. Classes are like the human world. We can create attributes, different abilities, common abilities. For example, we know that we can all talk, but some people are sick or born with defects and do not have the ability. We can take these abilities, as follows:
class People(object): '''Creating Human Beings''' def __init__(self,name,age,sex,blood_type): """People have the attributes of name, age, sex and blood group.""" self.name = name self.age = age self.sex = sex self.blood_type = blood_type def walk(self): '''Creating a Walking Method''' print("%s can walk!!!!" %self.name) def speak(self): """Create Speech Properties""" print("%s can speak!!" %self.name) def learn(self): '''Create learning attributes''' print("%s is learning." %self.name) p1 = People("wupeiqi",18,"female","0") p2 = People("Alex",8,"male","AB") p1.walk() p2.walk()
The operation is as follows:
wupeiqi can walk!!!!
Alex can walk!!!!
In the above code, we can see that Alex and wupeiqi would have run, but now suddenly there is a small situation, Alex will not run (of course, this is only hypothesis), as follows:
class People(object): '''Creating Human Beings''' def __init__(self,name,age,sex,blood_type): """People have the attributes of name, age, sex and blood group.""" self.name = name self.age = age self.sex = sex self.blood_type = blood_type def walk(self): '''Creating a Walking Method''' print("%s can walk!!!!" %self.name) def speak(self): """Create Speech Properties""" print("%s can speak!!" %self.name) def learn(self): '''Create learning attributes''' print("%s is learning." %self.name) p1 = People("wupeiqi",18,"female","0") p2 = People("Alex",8,"male","AB") print(People.walk," ",p1.walk) p1.walk() p2.walk() del People.walk # try: # delattr(p2,"walk") # except AttributeError: # print("Method can not be deleted!!!!?") p1.walk() p2.walk()
The results are as follows:
wupeiqi can walk!!!!
Alex can walk!!!!
Traceback (most recent call last):
File "/home/zhuzhu/Day 7/Human beings.py", line 37, in <module>
p1.walk()
AttributeError: 'People' object has no attribute 'walk'
As can be seen from the above code, the created instance can not delete the method in the class. The method in the class does not support deletion. Let's see how to operate on the attributes in the class.
We have made wupeiqi anonymous, as follows:
class People(object): '''Creating Human Beings''' def __init__(self,name,age,sex,blood_type): """People have the attributes of name, age, sex and blood group.""" self.name = name self.age = age self.sex = sex self.blood_type = blood_type def walk(self): '''Creating a Walking Method''' print("%s can walk!!!!" %self.name) def speak(self): """Create Speech Properties""" print("%s can speak!!" %self.name) def learn(self): '''Create learning attributes''' print("%s is learning." %self.name) p1 = People("wupeiqi",18,"female","0") p2 = People("Alex",8,"male","AB") print(p1.name,p2.name) del p1.name print(p1.name,p2.name)
The results are as follows:
wupeiqi Alex
Traceback (most recent call last):
File "/home/zhuzhu/Day 7/Human beings.py", line 29, in <module>
print(p1.name,p2.name)
AttributeError: 'People' object has no attribute 'name'
From the above results, we can see that we deleted the attributes in the class, that is, attributes can be modified and deleted, we let "wupeiqi" become anonymous, no longer have a name.
As can be seen from the above, in classes, we can only modify and delete the attributes of classes, not delete the methods of classes.
Classes are the same as humans. They encapsulate a general property. They can create innumerable individuals. Each individual has its own unique attributes. We can add attributes by adding attributes. There are also many different skills and methods. We can all achieve the perfection of individual instances in classes.
Like ourselves, we lack certain attributes that we can acquire through our own efforts.
Class is the description of the real world.
class People(object): '''Creating Human Beings''' def __init__(self,name,age,sex,blood_type): """People have the attributes of name, age, sex and blood group.""" self.name = name self.age = age self.sex = sex self.blood_type = blood_type def write(self,write_abality=False): '''Creating the ability to write, not everyone has the function of writing, so it needs to be judged.''' if write_abality == False: print("Sorry,%s is a illiteracy,so %s cann't have the ability of writing!" %(self.name,self.name)) elif write_abality == True: print("%s is a professional in an and literature,%s can write!" %(self.name,self.name)) else: print("Sorry,cann't identify the parameter of %s." %write_abality) def walk(self): print("%s can walking!") p1 = People("Alex",18,"Female","AB") p1.write("Alex")
We know that some people can write, some people can not write, so we have to judge whether this person has the attribute of writing, if there is, it can write; if not, it will not. So add a judgment. This allows you to see if you have write functionality.
There are many things in the real world that we need to learn, but the mastery of classes is still not complete, especially when we don't know how to describe the real world, which needs to be accumulated by ourselves.