Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the special method of python and how to use it

Shulou Source: shulou.com Published: 2022-05-31 17:43:42 10月14日 Update

Most people don't understand the knowledge points of this article "What is python special method and how to use it", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "What is python special method and how to use it".

description

In a class, methods that begin and end with__are special methods, and special methods have special uses. They can be called directly or indirectly via built-in functions or operators such as__init__(),__next__().

Examples of special methods

__init__() is used for initialization of objects.

__init__() is typically a special method used to initialize objects. Called automatically during class instantiation.

Next() calls iterator__next__()

Calling next() on the iterator generates the next value. Behind this procedure, next() calls the iterator__next__().

When len() is called, the container's__len__() method is automatically called.

Calling len() returns the length of a container. The reason is that the__len__() method is implemented in the container class, and when the len() function is called, the__len__() method of the container is automatically called.

Using print() will automatically call__str__.

example code

class A: def __str__(self): Python is a programming language with many effective tools built in. Python is almost omnipotent. The language is easy to understand, easy to get started, powerful, and widely used in many fields, such as the most popular big data analysis, artificial intelligence, Web development, etc.

The above is the content of this article about "what is python special method and how to use it". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will help everyone. If you want to know more relevant knowledge, please pay attention to the industry information channel.

Tags: Methods special content functions containers objects iterations examples that is articles knowledge articles languages processes powerful effective popular popular omnipotent manual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Microsoft OPPO Reno Shulou Tech Info