Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8558349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:47:29+00:00 2026-06-11T15:47:29+00:00

I have a simple vector class that overloards several arithmetic operators: class vec2: x

  • 0

I have a simple vector class that overloards several arithmetic operators:

class vec2:
    x = 0.0
    y = 0.0

    def __add__(self,other):
        self.x = other.x
        self.y = other.y

    def __mul__(self,scalar):
        self.x *= scalar
        self.y *= scalar

However, somewhere else I call the method like this:

class foo:
    position = vec2()
    velocity = vec2()

    def update(self,dt):
        self.position += self.velocity * dt;

However, once I get to the update function, the interpreter gives an error:

'tuple' object has no attribute 'x'

inside the __add__ function.

Why is “other” in __add__ passed as a tuple, and not a vec2?

The entire code is here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:47:31+00:00Added an answer on June 11, 2026 at 3:47 pm

    Return new vectors when using __add__ and __mul__, and handle ‘strange’ types:

    class vec2:
        x = 0.0
        y = 0.0
    
        def __init__(self, x=0.0, y=0.0):
            self.x, self.y = x, y
    
        def __add__(self, other):
            if not isinstance(other, self.__class__):
                return NotImplemented
            result = self.__class__(self.x, self.y)
            result.x += other.x
            result.y += other.y
            return result
    
        def __iadd__(self, other):
            if not isinstance(other, self.__class__):
                return NotImplemented
            self.x += other.x
            self.y += other.y
            return self
    
        def __mul__(self, other):
            if not isinstance(other, self.__class__):
                return NotImplemented
            result = self.__class__(self.x, self.y)
            result.x *= other.x
            result.y *= other.y
            return result
    
        def __imul__(self, other):
            if not isinstance(other, self.__class__):
                return NotImplemented
            self.x *= other.x
            self.y *= other.y
            return self
    

    To modify the vectors in-place, use __iadd__ and __imul__; these still need to return the new value; this can be self.

    Note that this does not handle just passing in a tuple of (x, y) coordinates. If you want to support that usecase, you need to specially handle it:

    class foo:
        def __init__(self, position=(0.0, 0.0), velocity=(1.0, 1.0)):
            self.position = vec2()
            self.velocity = vec2(*velocity)
    
        def update(self, dt):
            if isinstance(dt, tuple):
                dt = vec2(*dt)
            self.position += self.velocity * dt;
    

    Note also that you should not really use class attributes for your position and velocity values; I’ve used instance attributes instead above, and took the opportunity to set both position and velocity to sane values.

    Demo:

    >>> f = foo()
    >>> f.position.x, f.position.y
    (0.0, 0.0)
    >>> f.update((1, 2))
    >>> f.position.x, f.position.y
    (1.0, 2.0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class that's a simple vector, but this piece of code
Say you have a simple class with some storage data structure (list, vector, queue,
I have a relatively simple algorithm that walks an std::vector looking for two neighbouring
I have a custom Vector class that contains an x, y, and z. These
/* simple class that has a vector of ints within it */ class A
I have a simple class and I am using a std::vector to contain all
I have simple class with width and height member fields which define number of
I have simple php validation form that is halfway working. If you leave the
I'm writing a simple nD-vector class, but am encountering a strange bug. I've stripped
I have a very simple AS3 app that draws few circles using drawing api

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.