0

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.

3
  • Why would you want that? If you have a bunch of matlab code or data formatted in some other way, wouldn't an importer be less work than hacking the Python interpreter? In your examples, the first and last one have well defined semantics in Python already. It looks like your overloading would make your new language non-deterministic. Commented Feb 8, 2015 at 2:25
  • 1
    I'm doing it for the experience/fun I guess. I love python and I don't want to use Matlab and I think Matlab syntax is cool, so I'm like.... Hm, what if I do this in Python. It'll be a learning experience. Commented Feb 8, 2015 at 2:28
  • for ideas check out docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html Commented Feb 8, 2015 at 3:40

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.