David Tran
David Tran

Reputation: 47

Create a matrix class/object in python without having to call Matrix()

I'm learning Linear Algebra and I'm really excited about it and I'm trying to create a Matrix class/object in Python. The goal here is that I'll try to add more features to my matrix class the more I learn about in Linear Algebra.

However, instead of:

class Matrix():
    def __init__(self, etc)
   ....
mat = Matrix('1 2; 3 4')

Is there a way I can mess with the evaluator or the parser or the syntax so that I can do:

A = '1 2; 3 4' #or
A = [1 2; 3 4] #like matlab?
A = ['1 2; 3 4'] #if others is not possible

with A being a Matrix_object. Even if it's a bit hard, I'm willing to write it. I just don't know whether what I want to IS impossible in Python.

Upvotes: 0

Views: 1060

Answers (1)

tourdownunder
tourdownunder

Reputation: 1829

You want to overload the list operator to be able to use the []. The below example shows how to allow you to overload the [] for a particlar type though you can not use [].

How to override the [] operator?

To do what you want to do I believe you will need to change the python source code and compile it to change the [] functionality.

https://docs.python.org/devguide/setup.html

Upvotes: 1

Related Questions