[issue34314] Like __hash__, allow setting MyClass.__init__ to None to prevent it being called by type.__call__
Raymond Hettinger
report at bugs.python.org
Wed Aug 1 23:17:08 EDT 2018
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
> Right now, you really gotta jump through hoops
> in some cases if you only want to use __new__
> and don't care about __init__
I'm now sure I see the difficulty. It is easy to define a classes with __new__ and without __init__:
>>> class A:
def __new__(cls, *args):
print('New called with', cls, 'and', args)
return object.__new__(cls)
>>> a = A(10, 20, 30)
New called with <class '__main__.A'> and (10, 20, 30)
>>> isinstance(a, A)
True
> Like __hash__, allow setting MyClass.__init__ to None
FWIW, the API for hashing being set-to-None wasn't done because we like it (there a numerous downsides). It was done because we needed hashing to be on by default and there needed to be a way to turn it off. The downsides are that it confuses users, it is hard to teach and remember, and it adds a branch to various APIs which would then need to test for None. Instead, we prefer the pattern of having object() provide a default dummy method that either does nothing or gives up (like object.__init__ or the various rich comparison methods for object). This simplifies downstream code than doesn't have to check for a rare special case.
----------
assignee: -> rhettinger
nosy: +rhettinger
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34314>
_______________________________________
More information about the Python-bugs-list
mailing list