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

  • SEARCH
  • Home
  • 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 8768825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:10:01+00:00 2026-06-13T17:10:01+00:00

I want to implement my own matrix-class that inherits from numpy’s matrix class. numpy’s

  • 0

I want to implement my own matrix-class that inherits from numpy’s matrix class.

numpy’s matrix constructor requires an attribute, something like ("1 2; 3 4'"). In contrast, my constructor should require no attributes and should set a default attribute to the super-constructor.

That’s what I did:

import numpy as np

class MyMatrix(np.matrix):
    def __init__(self):
        super(MyMatrix, self).__init__("1 2; 3 4")

if __name__ == "__main__":
    matrix = MyMatrix()

There must be a stupid mistake in this code since I keep getting this error:

this_matrix = np.matrix()
TypeError: __new__() takes at least 2 arguments (1 given)

I’m really clueless about that and googling didn’t help so far.

Thanks!

  • 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-06-13T17:10:03+00:00Added an answer on June 13, 2026 at 5:10 pm

    Good question!

    From looking at the source, it seems as though np.matrix sets the data argument in __new__, not in __init__. This is counterintuitive behaviour, though I’m sure there’s a good reason for it.

    Anyway, the following works for me:

    class MyMatrix(np.matrix):
        def __new__(cls):
            # note that we have to send cls to super's __new__, even though we gave it to super already.
            # I think this is because __new__ is technically a staticmethod even though it should be a classmethod
            return super(MyMatrix, cls).__new__(cls, "1 2; 3 4")
    
    mat = MyMatrix()
    
    print mat
    # outputs [[1 2] [3 4]]
    

    Addendum: you might want to consider using a factory function, rather than a subclass, for the behaviour you want. This would give you the following code, which is much shorter and clearer, and doesn’t depend on the __new__-vs-__init__ implementation detail:

    def mymatrix():
        return np.matrix('1 2; 3 4')
    
    mat = mymatrix()
    print mat
    # outputs [[1 2] [3 4]]
    

    Of course, you might need a subclass for other reasons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement my own copy method for a class A which looks
I want to implement an action bar for my own android app like the
I want to implement a suggestion combobox which shows suggestions grabbed from my own
I want to implement a simple class for logging from multiple threads. The idea
I want to implement a dictionary that creates its own elements on the fly
How can I implement serialization on my own. Meaning I don't want my class
I am very new to Scala. I want to implement a generic matrix class
I want to implement my own realtime notification system (like facebook - when someone
I'm trying to implement my own kind method cache. For that, first I want
I have my own password/login rules that I want to implement such as: Password:

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.