Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the built-in methods and properties of Python

Shulou Source: shulou.com Published: 2022-06-02 06:55:27 09月29日 Update

This article focuses on "what are the built-in methods and properties of Python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the built-in methods and properties of Python.

1.1 _ del_ method (known)

In Python

When an object is created with the class name (), the _ init_ method is called automatically after the object has been allocated space

The _ del_ method is called automatically before an object is phased out of memory

Life cycle

1) an object is created from the calling class name (), and the life cycle begins

2) once the _ del_ method of an object is called, the life cycle ends

3) during the life cycle of the object, you can access the properties of the object or have the object call the method

Class Cat (): def _ _ init__ (self New_name): self.name = new_name print ("% s is coming"% self.name) def _ del_ (self): print ("% s I went"% self.name) # tom is a global variable tom = Cat ("Tom") print (tom.name) # del keyword can delete an object del tom print ("-" * 50) 1.2 _ str_ method

In Python, print is used to output object variables. By default, the object referenced by this variable is output from which class created the object, as well as the address in memory (hexadecimal representation)

If, in development, you want to print custom content when you want to use print to output object variables, you can take advantage of the built-in method _ str_

Note:

The _ str_ method must return a string

Class Cat (): def _ _ init__ (self New_name): self.name = new_name print ("% s is coming"% self.name) def _ del_ (self): print ("% s I went"% self.name) def _ _ str__ (self): # must return a string return "I am a kitten [% s]"% self.name # tom is a global variable tom = Cat ("Tom") print (tom) so far I believe that you have a deeper understanding of "what are the built-in methods and properties of Python?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Objects methods variables properties periods life content output global memory characters strings class names learning practical deeper keys keywords interests hexadecimal Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL Shulou Technology Huawei Microsoft