“This is the 17th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
The 40th official Python column, stop! Don’t miss this zero-based article!
In the previous article, the committee showed and shared the concepts of class inheritance and rewriting, and object orientation. Keep up with that!
Classes and objects are reviewed again
Classes and objects, classes are like model data in 3D modeling, and objects follow concrete models that are 3D printed using this 3D model.
Classes have no life cycle, because your code writes the class, it exists, not because it is not loaded!
But we didn’t load the class into PYTHONPATH, and it wasn’t found! But that doesn’t mean it doesn’t exist!
Objects? It exists only during the execution of a Python program. When the intermediate program is closed, the object disappears.
Objects have a life cycle
Objects are created, remembered, and destroyed. (Python has corresponding functions.)
Data sharing between classes and objects
It’s like a family living in the same house, the whole house is shared, so that it’s less than.
Take a look at the following program:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/11/15 11:58 PM
# @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(Student Committee Fan No. 2)
# 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 type :", p1.__class__)
print("PythonProgrammer namespace :", PythonProgrammer.__dict__)
print("PythonProgrammer class.", PythonProgrammer.__class__)
Copy the code
Make sure you understand the following results:
Shared variables/functions have the same address in memory.
Other functions and variables, after the class is loaded, the function is loaded, and the address of the function reference is not the same as the address of the object created!
At the same time, the data attributes of the class, no address reference (because the class is not assigned data attributes), only instantiated as objects, we can modify the properties of the object, the object is stateful!
As shown in the figure below, of course, the memory address is just a fiction, not necessarily continuous!
conclusion
Classes and objects, one is like the model data in 3D modeling, and the 3D printed model.
But there is no shared variable/shared function in 3D printing, because the model data file has no physical connection with the printed object!
For those who like Python, please check out the Python Basics section or the Python Getting Started to Master Section
Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!