You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug in documentation example code for subclassing ndarray #14386
The example for class C includes a call to object.__new__() with more than one argument:
classC(object):
def__new__(cls, *args):
print('Cls in __new__:', cls)
print('Args in __new__:', args)
returnobject.__new__(cls, *args)
Calling this results in an error:
File "/home/jon/python/ndarray_testing.py", line 31, in __new__
return object.__new__(cls, *args)
TypeError: object.__new__() takes exactly one argument (the type to instantiate)
If the extra argument to object.__new__() is removed, the example works as expected.
Fixed example:
classC(object):
def__new__(cls, *args):
print('Cls in __new__:', cls)
print('Args in __new__:', args)
returnobject.__new__(cls)
def__init__(self, *args):
print('type(self) in __init__:', type(self))
print('Args in __init__:', args)
Output of fixed example
In [129]: C('foo')
Cls in __new__: <class '__main__.C'>
Args in __new__: ('foo',)
type(self) in __init__: <class '__main__.C'>
Args in __init__: ('foo',)
Out[129]: <__main__.C at 0x7f27fb972710>
Numpy/Python version information:
1.16.2 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0]
Activity
DOC: fix issue with __new__ usage in subclassing doc.
rgommers commentedon Aug 28, 2019
thanks for the report @jonpovey. fix taken over in gh-14395
DOC: fix issue with __new__ usage in subclassing doc.