0

What is an example of using __new__ in Python such that within the body of __new__ you do not return the result of calling some other (base, meta, or otherwise) class's version of __new__, but you do return an allocated instance of the intended class so that __init__ will be invoked?

I'm guessing this is (and should be) very rare, but I'm curious if there is an example of this use case. I tried searching around for the Python source of tuple's implementation of __new__ and also for type's implementation, but it did not seem like this is quick and easy to find online.

ely
  • 74,674
  • 34
  • 147
  • 228
  • 2
    What do you mean by "an allocated instance"? There are things like a [Singleton](http://stackoverflow.com/a/1810367/1427416) that use `__new__` to return an existing instance of the same class, but `__new__` is still used to construct the initial instance, and re-calling `__init__` is arguably not even desirable there (although it happens in that example). There's no way to create a real new instance of a user-defined class without leveraging the builtin `object.__new__` at some point. `tuple` and `type` are implemented in C so they don't really use the same API with `__new__`. – BrenBarn Oct 19 '14 at 22:24
  • By "allocated instance" I just mean the thing that `__new__` returns. How do you make new instances of things without bottoming out at something else's `__new__`? Or is that a misguided question? – ely Oct 19 '14 at 22:43
  • You can't. You have to either call something else's `__new__` or just create an object by calling some other class/type, which under the hood will call that class's `__new__` too. – BrenBarn Oct 19 '14 at 22:46

1 Answers1

1

You can't, that's not something you can do on the Python side of things. A no-op __new__ method would call object.__new__(cls), which returns a bare instance of class cls.

>>> class A:
...     def __new__(cls):
...         return object.__new__(cls)
... 
>>> A()
<__main__.A object at 0x7f9e951577d0>
Aleksi Torhamo
  • 6,452
  • 2
  • 34
  • 44
  • Just to confirm, things like `tuple.__new__`, `object.__new__`, and `type.__new__` have their implementations at the C level, and define the only set of behaviors (at the root of it all) for `__new__`. For classes, at some level down the chain, you are relying on some built-in type's `__new__` (or `object`'s), and for metaclasses, the same but for `type`. Is there any kind of immutability in `__new__` besides sequence-type immutability for `tuple`? – ely Oct 20 '14 at 01:31
  • @EMS: What do you mean "kind of immutability"? – BrenBarn Oct 20 '14 at 01:42
  • Subclassing `tuple` is a way to get some custom things plus immutability, but not other class attributes. Using descriptors is one way to make things read only, but not legit immutable. I'd like something such that `object.__setattr__` will not work to adjust any attrs, but which perhaps has arbitrary attributes or things defined in `__new__`. – ely Oct 20 '14 at 02:25
  • @EMS: AFAIK it's not possible to get true immutability on user-defined classes (without deriving from existing immutable type) in Python without going to C. What do you mean with "other class attributes"? (wrt. the python code being hard to find, I forgot to mention you can find it [here](https://hg.python.org/cpython/)) – Aleksi Torhamo Oct 20 '14 at 02:49