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

How to compare init and new and analyze the singleton pattern

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

Share

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

Today, I will talk to you about how to compare init and new and analyze the singleton pattern. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

A brief summary is as follows:

The _ _ init__ _ method is the object's generation method, and the _ _ method is the object's initialization method.

2, the object can not be initialized until the object is generated. So the _ _ new__ method is called before the _ _ init__ method.

3The first parameter of the _ init__ _ method is cls, which refers to the class itself, and the first parameter of _ _ new__ is self, which refers to the object generated by the _ _ class method.

4The remaining parameters of the recently updated _ method continue to be passed to the _ _ init__ method along with the generated object.

5The return value of the recently updated _ method is usually generated by calling the _ _ new__ method of its parent class.

6 the method cannot have a return value.

7 less used, you can use it to implement the singleton pattern, that is, only one instance of a class can be created, and sometimes the memory footprint can be greatly reduced by using the singleton pattern.

The code used is as follows:

Class Book (object):

# object generation method

Def _ _ new__ (cls, title):

# designed in singleton mode

If not hasattr (cls, "_ instance"):

Cls._instance= super (). _ _ new__ (cls)

Print ('in _ _ new__')

Return cls._instance

# object initialization method

Def _ _ init__ (self, title):

Print ('in _ _ init__')

Super (). _ _ init__ ()

Self.title = title

If _ _ name__ = ='_ _ main__':

B = Book ('The Spider Book')

B2 = Book ('The Flask Book')

Print (id (b))

Print (id (b2))

Print (b.title)

Print (b2.title)

After reading the above, do you have any further understanding of how to compare init and new and how to analyze singleton patterns? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report