0

I'm trying to make a class with a variable parameter list, the code:

Vector.py class code:

class Vector(tuple):
    def __new__(cls, *V):
        return tuple.__new__(cls, V)
    def __init__(self, *V):
        self.V = V
    def __repr__(self):
        """An overloaded method that it writes a vector in a terminal."""
        return('({})'.format(self))

TestVector.py code:

from Vector import Vector


def main():
    'We create 2 Vectors'
    V1 = Vector((3, 2))
    V2 = Vector((4, 5))
    print(V1)
    print(V2)

When I execute the code I obtain none error message, only a white line.

Thanks

7
  • This gets me infinite recrusion when I run it... Commented May 7, 2014 at 22:54
  • Yeah, I removed the repr and new and code works like a charm Commented May 7, 2014 at 22:56
  • Apologies, you do need __new__ if you want to assign to a tuple like this. Commented May 7, 2014 at 23:00
  • 1
    It gets into an infinite recursion since __repr__ is called when you try to turn the Vector object into a string. Commented May 7, 2014 at 23:02
  • 1
    I'm not quite sure why 'repr' is needed... by default since the class is based on a tuple it prints out correctly to me. Commented May 7, 2014 at 23:04

2 Answers 2

1

So there appears to be some confusion with the way you are specifying your arguments.

You use:

def __init__(self, *V):

which implies (along with the title of your question) that you want to give a variable number of arguments, like:

V1 = Vector(3, 2)
V2 = Vector(1,2,3,4,5,6,7)

yet you are passing tuples to the Vector class like:

V1 = Vector((3, 2))
V2 = Vector((1,2,3,4,5,6,7))

This is not a variable parameter list, it is just one argument regardless of how long the tuple is.

As has been pointed out in the comments above, your overloaded __repr__ method is broken and will cause the code to infinitely recurse. There is no need to overload this method as the default one does exactly what you are asking.

Putting that all together, your code becomes:

class Vector(tuple):
    def __new__(cls, *V):
        return tuple.__new__(cls, V)

    def __init__(self, *V):
        self.V = V

def main():
    'We create 2 Vectors'
    V1 = Vector(3, 2)
    V2 = Vector(4, 5)
    print(V1)
    print(V2)

main()

The only question that remains is why you would want to do this all in a subclass of tuple, rather than using (for instance) numpy.

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

1 Comment

Well, I'm a mathematician, so I would want to practise with a class with a variable list of parameters and vectors is funny to me. I know numpy ;-) Yes, I forgot main(), I'm a stupid, I know ;-)
0

Remove __repr__and__new__ and your code should work fine.

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.