14

I understand what __new__ does (and how it's different from __init__) so I'm not interested in definitions, I'm interested in when and how to use __new__.

The documentation says:

In general, you shouldn't need to override __new__ unless you're subclassing an immutable type like str, int, unicode or tuple

But I can't think of other cases to use __new__ or how to use it correctly (for example when subclassing an immutable type or why it's needed in this case).

So, when, why and how do you need to use __new__?

I'm interested in the use cases, not what it does (I know what it does).

Lauritz V. Thaulow
  • 49,139
  • 12
  • 73
  • 92
JohnDoDo
  • 4,720
  • 8
  • 31
  • 47
  • 2
    @McFarlane: I know that, I'm interested on the situations when you need to use __new__ (more than that phrase of "subclassing an immutable type". **This is not a duplicate.** – JohnDoDo Oct 11 '12 at 08:27
  • 1
    @close voters This is not a duplicate. The referenced question asks *why `__init__` is always called after `__new__`* (it's not). This question asks for practical use cases for `__new__`. An answer for this question is not an answer for that question. – Lauritz V. Thaulow Oct 11 '12 at 08:54
  • @lazyr: hope you don't mind, I incorporated your comment into the question. Thanks! – JohnDoDo Oct 11 '12 at 09:45
  • What, *another* close voter? Please *read* the text. It says: "This question covers *exactly* the same content as earlier questions on this topic. Its answers *may be merged with* another identical question" (emphasis mine). **This is not the case.** The answers in the combined question will look out-of-place! – Lauritz V. Thaulow Oct 11 '12 at 11:26
  • You'll find very good use case in [What is a metaclass in Python?](http://stackoverflow.com/questions/100003/) – Piotr Dobrogost Oct 11 '12 at 17:51
  • @Tichodroma Did you read the question, the alledged duplicate, the comments, and close reason description? Would you care to explain why you still thought it appropriate to close this question? – Lauritz V. Thaulow Oct 12 '12 at 11:01
  • @lazyr As far as I can tell, this is a duplicate of http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init At leaset four other users thought so, too. If this ist wrong, please vote to reopen the question. –  Oct 12 '12 at 11:06
  • @Tichodroma So in other words, you honestly think [this answer](http://stackoverflow.com/a/12835637/566644) may be merged in as an answer to [this question](http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init)? – Lauritz V. Thaulow Oct 12 '12 at 19:46
  • @lazyr I voted to close the question when it was called 'Python - When, why and how do you need to use `__new__`'. The [accepted answer](http://stackoverflow.com/a/674369/647772) to the referenced question answers this original title. So, yes, I think this here is a duplicate. –  Oct 12 '12 at 19:55
  • @Tichodroma First, it's not enough to read the title to determine if it's a duplicate. The first sentence after the title was this: *"I understand what `__new__` does (and how it's different from `__init__`)"*. Second, even if it was a duplicate then (and it wasn't), it isn't now, so you *should* vote to reopen. – Lauritz V. Thaulow Oct 13 '12 at 11:37
  • @lazyr I've voted to reopen this question. Can we settle this now? –  Oct 13 '12 at 13:55
  • @Donal Fellows Could you also please consider undoing your close vote? – Lauritz V. Thaulow Oct 13 '12 at 19:37
  • @BasicWolf I'd like you as well to please read the comments and see if you're still convinced this is a duplicate. – Lauritz V. Thaulow Oct 13 '12 at 19:39

2 Answers2

14

Answering for myself, I've used it to

  • create a singleton pattern (though there are ways to do that without using __new__)
  • dynamically extend classes from external modules without actually editing the source
  • customize classes in a metaclass (though __call__ may also be used I think)
  • extend a the datetime.datetime class (which is immutable) to return the current time if instanciated without arguments and the result of calling strptime on the argument if called with a single string argument

You need __new__ when subclassing an immutable type if you want to alter the arguments used to construct the immutable, as I wanted in the datetime example, or if you don't want to call the parent __new__ at all but return an instance created another way or even of an entirely different type. By the time you're in __init__ it's too late to alter the object in any way, so you need to do it in __new__.

Community
  • 1
  • 1
Lauritz V. Thaulow
  • 49,139
  • 12
  • 73
  • 92
2

A potential use case could be a "factory class" which returns instances of various classes depending on the implementation.

glglgl
  • 89,107
  • 13
  • 149
  • 217