Coding exception classes, I came across this error:
TypeError: object.__new__(A) is not safe, use Exception.__new__()
There's a similar question posted here:
TypeError: object.__new__(int) is not safe, use int.__new__(). So __new__
was deprecated for the following reason:
[Python-Dev] __new__
deprecation
Guido van Rossum
"The message means just what it says. :-) There's no point in calling
object.__new__()
with more than a class parameter, and any code that did so was just dumping those args into a black hole."
But the warning in 3.3 that I get "is not safe" is scary. I try to understand the implication of using object.__new__
, let's consider this example:
>>> class A(Exception):
... def __new__(cls, *args):
... return object.__new__(A)
...
>>> A()
TypeError: object.__new__(A) is not safe, use Exception.__new__()
Fails miserably. Another Example:
>>> class A(object):
... def __new__(cls, *args):
... return object.__new__(A)
...
>>>
>>> A()
<__main__.A object at 0x0000000002F2E278>
works fine. Although, object
is a builtin class just like Exception
with respect to their roles, they share the trait of being builtin-classes. Now with Exception
, the first example raises TypeError
, but with object
, it does not?
(a) What are the downsides of using object.__new__
that made Python to raise the error (TypeError:...is not safe...
) in the first Example?
(b) What sort of checking Python performs before to calling __new__
? Or: What is the condition that makes Python raise the error in the first example?