Vector multiplication python

Hello im learning python and i get class topic. I recived a error message like this:

"TypeError: __init__() missing 1 required positional argument: 'y'"

this is my code from shell:

class Vektor():
    """ Bu bir vektör sınıfıdır"""
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def boyu(self):
        boy = (self.x**2+self.y**2)**0.5
        return boy
    def __repr__(self):
        return ("%di + %dy" %(self.x, self.y))
    def __sub__(self,digeri):
        return Vektor(self.x - digeri.x, self.y - digeri.y)
    def __add__(self,digeri):
        return Vektor(self.x + digeri.x, self.y + digeri.y)
    def __eq__(self,digeri):
        if self.boyu() == digeri.boyu(): return True
    def __mul__(self,digeri):
        self.x = Vektor(self.x + digeri.x)
        self.y = Vektor(self.y + digeri.y)
        return Vektor(self.x*digeri.x,self.y*digeri.y)

When i try to create a C = A*B like this i get error:

TypeError: init() missing 1 required positional argument: 'y'

Im already thank you and i want to remind im a newbite in programming :))

Upvotes: 0

Views: 1315

Answers (3)

Useless
Useless

Reputation: 67743

How many arguments does Vektor.__init__ require? Ignoring self, it's two - x and y.

When you wrote return Vektor(self.x*digeri.x,self.y*digeri.y), you passed two arguments, so this works.

When you wrote self.x = Vektor(self.x + digeri.x), this doesn't work, because you don't pass a second argument for the y value.

When Python gave you the error, it should have included a line number, which is supposed to show where the error occurred. You didn't include that, but it was this line, wasn't it?

Since Vektor is supposed to contain two scalars and not sometimes replace them with two vectors, you could just write

    self.x = self.x + digeri.x # still not a vector
    self.y = self.y + digeri.y # also not a vector

but the more important lesson is to read the error message carefully. Everything you needed was there.


A note on operator semantics: since you wouldn't normally expect an expression like x = v * w to modify x, you shouldn't be mutating self inside the operator function anyway.

You return the resultant vector, which is enough. I showed you how to fix the syntax of those two lines, but you should really just remove them entirely.

And another note on vectors: overloading * isn't such an obvious win as it is for a scalar numeric type, because vectors usually have more than one possible type of product.

Upvotes: 2

j-i-l
j-i-l

Reputation: 10957

In __mul__ you do for some reason:

self.x = Vektor(self.x + digeri.x)

which is calling Vektor.__init__ providing only the positional argument x, with the value self.x + digeri.x, but nothing for y, thus the error. Also this attempts to change the attribute x into an object from Vektor itself, I can't imagine that this is somehow what you want.

To me it is not clear what the 2 lines before the return statement in your __mul__ are supposed to do. Also both lines will produce the error you see.

Should your __mul__ be the dot product? If so, try:

return self.x*digeri.x + self.y*digeri.y

Another simplification to your class could be to allow iteration on your coords, like:

@property
def coords(self):
    return self.x, self.y

def __iter__(self):
    return self.coords.__iter__()

Then your dot product might just look like:

def dot(self, w):
    """ 
    The dot product of self and other vector w.
    """
    return sum([xi_s * xi_w for xi_s, xi_w in zip(self, w)])

There is VecPy, a very simple example class that does this kind of things. Maybe having a look can give you some more ideas.

Upvotes: 1

Siong Thye Goh
Siong Thye Goh

Reputation: 3586

The error is due to

self.x = Vektor(self.x + digeri.x)

When we call Vector like the way you write the syntax, it is thinking that you want to initialize it and it is expecting two inputs. Just get rid of the first two lines of mul function should fix the problem.

Upvotes: 0

Related Questions