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.

David Tran
  • 47
  • 1
  • 6
  • 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. – John Schmitt Feb 08 '15 at 02: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. – David Tran Feb 08 '15 at 02:28
  • for ideas check out http://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html – tourdownunder Feb 08 '15 at 03:40

1 Answers1

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

Community
  • 1
  • 1
tourdownunder
  • 1,779
  • 4
  • 22
  • 34