Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3798830
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:38:10+00:00 2026-05-19T13:38:10+00:00

[Python 3] I like ndarray but I find it annoying to use. Here’s one

  • 0

[Python 3]

I like ndarray but I find it annoying to use.

Here’s one problem I face. I want to write class Array that will inherit much of the functionality of ndarray, but has only one way to be instantiated: as a zero-filled array of a certain size. I was hoping to write:

class Array(numpy.ndarray):
  def __init__(size):
    # What do here?

I’d like to call super().__init__ with some parameters to create a zero-filled array, but it won’t work since ndarray uses a global function numpy.zeros (rather than a constructor) to create a zero-filled array.

Questions:

  1. Why does ndarray use global (module) functions instead of constructors in many cases? It is a big annoyance if I’m trying to reuse them in an object-oriented setting.

  2. What’s the best way to define class Array that I need? Should I just manually populate ndarray with zeroes, or is there any way to reuse the zeros function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:38:11+00:00Added an answer on May 19, 2026 at 1:38 pm

    If you don’t like ndarray interface then don’t inherit it. You can define your own interface and delegate the rest to ndarray and numpy.

    import functools
    import numpy as np
    
    
    class Array(object):
    
        def __init__(self, size):
            self._array = np.zeros(size)
    
        def __getattr__(self, attr):
            try: return getattr(self._array, attr)
            except AttributeError:
                # extend interface to all functions from numpy
                f = getattr(np, attr, None)
                if hasattr(f, '__call__'):
                    return functools.partial(f, self._array)
                else:
                    raise AttributeError(attr)
    
        def allzero(self):
            return np.allclose(self._array, 0)
    
    
    a = Array(10)
    # ndarray doesn't have 'sometrue()' that is the same as 'any()' that it has.
    assert a.sometrue() == a.any() == False
    assert a.allzero()
    
    try: a.non_existent
    except AttributeError:
        pass
    else:
        assert 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use some features of python like as Tuples and Sets in
Is there something like python's interactive REPL mode, but for Java? So that I
I'd like to make a class extending the numpy array base type, class LemmaMatrix(numpy.ndarray):
Are there database testing tools for python (like sqlunit)? I want to test the
Is there some string class in Python like StringBuilder in C#?
I have heard before that many Python developers don't use an IDE like Eclipse
I've seen a number of postgresql modules for python like pygresql, pypgsql, psyco. Most
When I saw this article about using a python-like sintax (indentation based, without curly
Is there something like Python's getattr() in C#? I would like to create a
An idiom commonly used in OO languages like Python and Ruby is instantiating an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.