What should be paid attention to when using _ _ new__ in python
Editor to share with you what you need to pay attention to when using _ _ new__ in python, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.
1. The first parameter cls is a class that is being instantiated.
To get an instance of the current class, the method should call the _ _ new__ () method of the parent class of the current class in the _ _ new__ () method statement of the current class. For example, if the current class inherits directly from object, the object returned by the _ _ new__ () method of the current class should be:
Class Foo (object): def _ _ new__ (cls,*args,**kwagrs): return object.__new__ (cls,*args,**kwagrs)
2. For the _ _ new__ () method of any new class, you cannot call _ _ new__ () itself to create an instance, because doing so will create an endless loop.
Class Bar (object): def _ _ new__ (cls,*agrs,**kwagrs): return Bar.__new__ (Foo,*agrs,**kwagrs) above is all the contents of this article entitled "what to pay attention to when using _ _ new__ in 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!