8000 Vector2: += mutates in-place while = creates new instance · Issue #4516 · pygame/pygame · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Vector2: += mutates in-place while = creates new instance #4516
Open
@TobiasPartzsch

Description

@TobiasPartzsch

Environment:

  • Operating system: (Linux(Ubuntu))
  • Python version: 3.13.3
  • SDL version: SDL 2.28.4
  • PyGame version: 2.6.1
  • Relevant hardware: N/A

Current behavior:

Vector2 in-place operators behave inconsistently. The += operator mutates the original Vector2 object (preserves object identity), while the *= operator creates a new Vector2 object (changes object identity). This inconsistency breaks expectations about object identity in game development.

  • or * operators also create a new object

Expected behavior:

In-place operators (+=, *=) should behave consistently with each other - either both should mutate the original object OR both should create new objects. Based on how regular operators (+, *) both create new objects, the expected behavior would be for in-place operators to also behave the same way as each other. (Creating a copy is probably safest.)

Steps to reproduce:

  1. Create two Vector2 objects
  2. Use += and *= operators
  3. Check object identity with id()
  4. Observe inconsistent behavior

Test code

If possible add a simple test program that shows the problem described in this report.

import pygame

v1 = pygame.Vector2(1,1)
v2 = pygame.Vector2(1,1)

print(f"id(v1): {id(v1)}")
print(f"id(v2): {id(v2)}")
print(f"id(v1 + v2): {id(v1 + v2)}")
print(f"id(v1 * v2): {id(v1 * v2)}")
v1 += v2
print(f"id(v1) after +=: {id(v1)}")
v1 *= v2
print(f"id(v1) after *=: {id(v1)}")```

**Stack trace/error output/other error logs**

pygame 2.6.1 (SDL 2.28.4, Python 3.13.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
id(v1): 135633051290384
id(v2): 135633051290336
id(v1 + v2): 135633051290624
id(v1 * v2): 135633043523536
id(v1) after +=: 135633051290384
id(v1) after *=: 135633043523536

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0