Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the magic methods developed by Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what are the magic methods of Python development", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the magic methods of Python development" this article.

Magic methods are special methods that can add magic to your class. if your object implements (overloads) one of these methods, then this method will be called by Python in special cases, and you can define the behavior you want, and it all happens automatically. They are often named by two underscores (such as _ _ init__, _ _ lt__), and the magic methods of Python are very powerful, so it is particularly important to understand how to use them!

_ _ init__ constructor, the method to initialize when an instance is created. But it is not the first method to instantiate the call.

_ _ new__ is the first method called by the instantiated object, which only takes down the cls parameter and passes the other parameters to _ _ init__. _ _ new__ is rarely used, but there are scenarios where it fits, especially if the class inherits from a type that doesn't change very often, such as tuples or strings.

_ _ call__ allows an instance of a class to be called like a function. _ _ getitem__ defines the behavior of getting the specified element in the container, which is equivalent to self [key]. _ _ getattr__ defines the behavior when a user tries to access an attribute that does not exist. _ _ setattr__ defines the behavior when a property is set. _ _ getattribute__ defines the behavior when an attribute is accessed.

These are all the contents of this article entitled "what are the Magic methods developed by Python?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report