error while creating objects in python

276 views Asked by At
class polynomial:
    def __init__(self, *coeff):
            self.coeff = coeff
    def __repr__(self):
            return 'polynomial(*{!r})'.format(self.coeff)
    def __add__(self, other):
            return polynomial(*(x + y for x, y in zip(self.coeff, other.coeff))                                                             
g = polynomial(1, 2, 3)
d = polynomial(3, 3, 4)

I'm trying to learn python and now I am trying to get the hang of classes and I have this Error raised g = polynomial(1, 2, 3) ^ SyntaxError: invalid syntax

1

There are 1 answers

1
Quickbeam2k1 On BEST ANSWER

For me this, works perfect, after adding a closing bracket in the __add__ function.

I can reproduce you error in a single jupyter notebook cell. However, if you separate the class definition from the instantiation in two different cells, you'd have obtained

return polynomial(*(x + y for x, y in zip(self.coeff, other.coeff))
                                                                   ^
SyntaxError: unexpected EOF while parsing

By the way, here a minimal complete and verificable example is usually required. If you would have tried to construct one, you typically would have included only the __init__ function. Iterating a bit you might have come to the error in the __add__ function. So performing such a procedure in the future, will improve your understanding an increase the chance of getting a good answer