F
F
frankwolf2018-10-06 20:11:39
Python
frankwolf, 2018-10-06 20:11:39

How does __new__ work in Python 3?

Please explain how the __new__ method works in Python 3, preferably with examples. What should I write in return so that it returns an instance of my class. I was looking for solutions to my question, a lot for Python 2, the documentation is not clear, there are no examples. Help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Dunaev, 2018-10-11
@Pasha13666

__new__(cls, *args, **kwargs) - always a classmethod. Called to create an instance of the class, before calling __init__. A new instance is created by calling object.__new__(cls) (you can also use super() instead of explicitly specifying object). It should be returned by __new__.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question