Home  >  Article  >  Backend Development  >  Understanding class and object in Python3 (code example)

Understanding class and object in Python3 (code example)

不言
不言forward
2018-10-27 16:00:059955browse

The content of this article is about the understanding of classes and objects in Python3 (code examples). It has certain reference value. Friends in need can refer to it. I hope it will help You helped.

What is a class
A class is an abstract concept that generally refers to things that are composed of many similar individuals and have common characteristics. For example, the class is a car
, and this car includes various models
What is an object
A specific thing in the class. For example, the Wuling Shenche S (the joke about the GTR being dropped in seconds when cornering is still fresh in my memory) is the embodiment of the car class.
First there are classes, then there are objects

Understand classes and objects

def eating():
    print("吃草")
    
def walking():
    print("走路")

eating()
walking()
#(输出)    吃饭
        走路
    
#这样的话,别人也可以调用你写的函数,但是调用eating() 并不能清除的了解到到底是谁 “吃饭”,
#所以我们输入一个name来明确是谁在吃草。

def eating(name):
    print(name,"吃饭")
eating("zhuozi")            #调用函数
#(输出)zhuozi 吃草

#But you will find that it is logically inappropriate for zhuozi to be used for eating. Although it is grammatically fine,
#so we will classify it into the category of "eating" Divided into one category, that is, category, so we mark it, class Person:
#Why the capital P is used to distinguish it from the classes of Python’s built-in functions. The built-in class names are all lowercase letters (you can ctrl the mouse Left-click the built-in function to view)

class Person:            #这样我们就将eating封装到了Person这个类里面
                        #只要是Person这一类别的都可以进行下面的行为(eating)
    def eating(name):
        print(name,"吃饭")
        
zhangsan = Person()        #这里就不能像以前一样用eating去调用了,因为只有Person这一类的才能就行eating这个行为
                        #所以我们就要先将zhansan定义为Person这一类    
zhangsan.eating()        #然后才能让张三进行吃草这种行为
#(输出)<__main__.Person object at 0x000001D2CCC21160> 吃饭

lisi = Person()            #lisi也要吃饭,那继续定义lisi 是person 类 他可以吃饭
lisi.eating()            #lisi就可以吃饭了
#(输出)<__main__.Person object at 0x000001BCC6BF1198> 吃饭

#打印的一个地址,一个类的地址 object(翻译就是对象)就表示 lisi是这个类的对象
#lisi 就是 Person这个类 实例化的对象。

Before we def eating(name): the name can be seen as who is eating, and the rigid requirement of the grammar here is to use self to represent who is calling eating. So as follows:

class Person:
    def eating(self):
        print(self,"吃饭")
class Person:
    pass
========
list  这里我们ctrl + 鼠标左键点击pycharm 里面书写的list,如图:

Understanding class and object in Python3 (code example)
It can be seen that the class names of the built-in functions are all lowercase, and the class we used before (list name).append() is what we are talking about now When calling functions under a class, the object name in parentheses is self and the list name is the instantiated object.

Syntax

class 类名(父类列表):        #类名和变量名称的命名是一样的,类名首字母要大写,
                            #用来区别是否是内置函数自带的类
    类体
    
数据属性                        #类的共同属性,名字,性别,干什么的,等等

方法属性                        #这个类共同都有的功能,跑,跳,计算,记录等等
class Person:
    pass

zhangsan = Person()                #张三是Person实例化的对象,所以要开辟一个内存来存储,
                                #即将zhangsan变量名指向这个内存地址
print(zhangsan)
#(输出)<__main__.Person object at 0x00000194D8D28400>

既然实例化对象后开辟了内存,我们就要在里面进行一个存储
class Person:
    pass

zhangsan = Person()
zhangsan.name = "zhangsan"                #给这个实例化的对象附一个名字
print(zhangsan)
#(输出)<__main__.Person object at 0x0000018DF6598400>

print(zhangsan.name)                    #打印出这个对象的名字
#(输出)zhangsan

zhangsan对象名 指向  内存,内存里面有一个属性name,而name指向zhangsan这个名字  两个zhngsan 不一样啊
zhangsan(类的对象名字)  -- 》内存 (name)----》zhangsan(对象的属性)

lisi = Person()                    #再实例化一个对象
lisi.name = "xiaolong"
print(lisi.name)    
print(id(lisi.name))            
lisi = Person()
lisi.name = "xiaolong"

print(id(zhangsan))
#(输出)2070146453856
print(lisi.name)
#(输出)xiaolong
print(id(lisi.name))
#(输出)2070146521264
It can be seen that the IDs of the two instantiated objects are different

Here comes the namespace rules:
Every Instantiating an object will occupy separate memory storage of the system if no special processing is performed.

class Person:
    pass

zhangsan = Person()                    
zhangsan.name = "zhangsan"
zhangsan.sex = "男"
zhangsan.age = 19
zhangsan.hight = 175

lisi = Person()
lisi .name = "xiaolong"
lisi .sex = "女"
lisi .age = 29
lisi .hight = 170
#两个内容的创建方式相同啊,只是内容不同,那么我们写一个函数来进行这种重复性的操作,
class Person:
    def __init__(self, name, sex, age, hight):
        self.name = name        zhangsan.name = name
        self.sex = sex            zhangsan.name = sex
        self.age = age            zhangsan.name = age
        self.hight = hight        zhangsan.name = hight
#self 是为了规范明确是谁调用了,所以相当于将zhangsan这个对象传给了self。
#而后面self之后的name, sex, age, hight 就是所要传入的"zhangsan" "男"19 175 信息
#zhangsan = Person()                
#zhangsan.name = "zhangsan"
#zhangsan.sex = "男"
#zhangsan.age = 19
#zhangsan.hight = 175

这样我们就不用再像上面一样传入了
zhangsan = Person("zhangsan","男","19",175)
print(zhangsan.name)
#(输出)zhangsan

init: It is a special function that exists in the system. When we instantiate an object (zhangsan = Person("zhangsan","male","19",175)) , the default instantiated object calls this function. def init(self, name, sex, age, hight):
The self object is named zhangsan before the equal sign,
and name, sex, age, hight correspond to "zhangsan"," Male","19",175

The above is the detailed content of Understanding class and object in Python3 (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete