current position:Home>Python object-oriented programming 05: concluding summary of classes and objects

Python object-oriented programming 05: concluding summary of classes and objects

2022-02-01 00:32:19 Lei Xuewei

「 This is my participation 11 The fourth of the yuegengwen challenge 17 God , Check out the activity details :2021 One last more challenge

ceremonial Python Column No 40 piece , Classmate, stop , Don't miss this from 0 The beginning of the article !

In the previous article, the School Committee showed and shared the inheritance and rewriting of classes , There are also some concepts of object orientation , Keep up !

Class and object recognition review

Classes and objects , Class is like 3D Modeling inside model data , Object with this 3D Model USES 3D Printed Concrete models .

Classes have no lifecycles , Because your code has written the class , It exists , Does not exist because it is not loaded !

But we didn't load the class into PYTHONPATH in , It's not found ! But that doesn't mean it doesn't exist !

What about objects? ? It only exists in Python During the operation of the program , middle The program is closed , The object disappears .

Objects have lifecycles

Object created , Be remembered ( Use ), Destroyed .(Python There is a corresponding function )

Data sharing between classes and objects

It's like a family living in a house , The facilities of the whole house are shared , This analogy .

Look at the following program :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/15 11:58  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello

class Value(object):
    pass

"""  Here is a programmer class definition  """


class PythonProgrammer(object):
    class_value = Value()

    def __init__(self, name):
        self.name = name

    def code(self):
        print(f"{self.name}: life is too short, why not python?")

 @staticmethod
    def learn(self):
        print(f"{self.name}: learn python")

    def __str__(self):
        return f"PythonProgrammer(name:{self.name})"

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.name == other.name
        return False


p1 = PythonProgrammer(" School Committee fans 2 Number ")
# p.code() #TypeError: code() missing 1 required positional argument: 'lang'
p1.code()
print("pi id:", id(p1))
print("PythonProgrammer id:", id(PythonProgrammer))
print("p1:", p1.name)
print("class_value id:", id(p1.class_value))
print("PythonProgrammer.class_value id:", id(PythonProgrammer.class_value))
print("p1.__dict__:", p1.__dict__)
print("p1.code:", p1.code)
print("PythonProgrammer.code:", PythonProgrammer.code)
print("p1.learn:", p1.learn)
print("PythonProgrammer.learn:", PythonProgrammer.learn)
print("p1 The type of :", p1.__class__)
print("PythonProgrammer Namespace :", PythonProgrammer.__dict__)
print("PythonProgrammer class :", PythonProgrammer.__class__)
 Copy code 

The following operation results must be understood :

 Screenshot  2021-11-20  In the morning 12.24.05.png

The meaning of the school committee here is as follows , Shared variables / function , Same address in memory .

Other functions and variables , After the class is loaded , The function is loaded , The function reference address is different from the address of the created object !

At the same time, the data properties of the class , No address references ( Because the class has no data attribute assignment ), Just instantiate as an object , We can modify properties through objects , Objects are stateful !

Just like the picture below , Of course, the memory address is just fictitious , Not necessarily continuous !

 Screenshot  2021-11-20  In the morning 12.54.53.png

summary

Classes and objects , One is like 3D Model data in modeling , Follow 3D Printed model .

however 3D There are no shared variables for printing / Shared function , Because the model data file , There is no material connection with the printed object !

like Python Friend, , Please pay attention to Python Basic column or Python From getting started to mastering the big column

Continuous learning and continuous development , I'm Lei Xuewei !
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !

copyright notice
author[Lei Xuewei],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010032180092.html

Random recommended