24, python object-oriented programming constructor and destructor

Insert picture description here
Insert picture description here
Python provides a function dir, which can pass objects or class names. It can print out all the members of this class.

print("Test Class")

class Video(object):
    # 构造函数
    def __init__(self):
        print("Create Video")
    
    # 析构函数
    def __del__(self):
        print("Delete Video") 

# <class '__main__.Video'>   其中的这个__main__是入口模块名称
video1 = Video()
print(Video)
print(video1)
print(dir(video1))
print("end")

Program output:

Test Class
Create Video
Delete Video
<class '__main__.Video'>
<__main__.Video object at 0x000001E5ADF2CA58>
['__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
end

<class' main .Video'>
where this __main__ corresponds to the name of the module, because we are a Python program, in the future we will write programs with multiple files, multiple files will involve one module for each file, and multiple For files, you have to know which file to start execution from, so there must be an entry file. In our Python, the entry is a module, which is the __main__ module, so the classes under this module are all __main__. class names.

Guess you like

Origin blog.csdn.net/zhaopeng01zp/article/details/109287613