程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Basic knowledge of Python construction class

編輯:Python

Reference resources :

python Define class initialization methods -Python Learning network

Popular explanation Python Medium __new__() Method _SJ2050 The blog of -CSDN Blog _python__new__

summary :

python There is no class constructor in .

__new__ and __init__ Matching is python The real class constructor in .

__new__ Create an instance ,__init__ Is to initialize an instance .

When an instance is generated , Execute first __new__, Create an instance , Re execution __init__ To initialize this instance

One 、__init__ initialization

1、 When the class is initialized , Methods in class __init__ Can be defined directly , It executes when the instance is generated , And the methods in the class are very different from ordinary functions .

2、 Methods in a class must contain a keyword self, That is to say instance In itself .

This keyword can be any variable name , Only commonly used self.

example

Declare a class , And add initialization methods for the class .

# Defining classes
class MyClass:
# Initialization method , The first parameter is the object itself self, The second parameter is the parameter that must be passed during instantiation
def __init__(self, name):
print(name)
# Class , Just pass it on name Parameters can be
c = MyClass(" Eraser ")

Two 、__new__() Create examples

class CapStr(str):
def __new__(cls, string):
self_in_init = super().__new__(cls, string)
print(id(self_in_init))
return self_in_init
def __init__(self,string):
print(id(self))
a = CapStr("I fu Ch!")
print(id(a))

1、__new__ The first argument to the method is this class , The rest of the parameters will be passed to __init__ Method initialization

We compare the parameters of the two methods , You can find __new__ The method is to pass in the class (cls), and __init__ Method to pass in the instantiated object of the class (self).

And here's the interesting thing ,__new__ The value returned by the method is an instantiated object (ps: If __new__ Method returns None, be __init__ Method will not be executed , And the return value can only call __new__ Method , You can't call a class that has nothing to do with it __new__ Method ).

We can understand their relationship in this way ,__new__ It's the general who opened up the territory , and __init__ It's the small people who work hard in this territory , Only __new__ After the execution , After opening up the territory ,__init__ To work , Incorporated into the code , That is to say __new__ The return value of is __init__ in self.
 

3、 ... and 、__del__ 

1、__del__ The method is called deconstruction method , It is used to implement the operations required for the object to be destroyed .Ex: Release the resources occupied by the object 、 Open file resources 、 Network connection, etc. .

Python Realize automatic garbage collection , When an object is not referenced ( The reference count is 0) when , The garbage collector calls __del__ Method .

2、 use del Statement to delete an object , Make sure that the system automatically provides __del__ Method , Generally, there is no need to customize the destructor method .

example :

class Person:
def __del__(self):
print(" Destroy object :{0}".format(self))
p1 = Person() # 5. Destroy object :<__main__.Person object at 0x000001DFCD279FC8>
print(id(p1)) # 1. 2060731260872
p2 = Person() # 3. Destroy object :<__main__.Person object at 0x000001DFCD284088>
print(id(p2)) # 2. 2060731302024
del p2
print("over") # 4. over
# print(id(p2)) # name 'p2' is not defined

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved