Skip to content

bug in documentation example code for subclassing ndarray #14386

Closed

Description

@jonpovey

There seems to be a minor bug in the example code for subclassing ndarray in this section of the online documentation

The example for class C includes a call to object.__new__() with more than one argument:

class C(object):
    def __new__(cls, *args):
        print('Cls in __new__:', cls)
        print('Args in __new__:', args)
        return object.__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:

class C(object):
    def __new__(cls, *args):
        print('Cls in __new__:', cls)
        print('Args in __new__:', args)
        return object.__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

  1. added a commit that references this issue on Aug 28, 2019
    983303f
  2. rgommers commented on Aug 28, 2019

    @rgommers
    Member

    thanks for the report @jonpovey. fix taken over in gh-14395

  3. added a commit that references this issue on Sep 6, 2019
    a22f391
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions