0

Here is a Particle class. When I initialize the different particles "byPosition" method, the particles shared the "self.position" variable, anyone have any ideal about why the "self.position" variable is shared?

import random
from PyQt5.QtCore import QPointF
from PyQt5.QtGui import QPen, QColor

class Particle:
    seed = False

    def __init__(self, velocity, position, h, seed):
        self.hue = h

        self.acceleration = QPointF(0, 0)
        self.velocity = velocity
        self.position = position
        self.seed = seed

    @classmethod
    def byXY(cls, x, y, h):
        lowVel = -11 * (y / 480)
        highVel = -5 * (y / 480)
        velocity = QPointF(0, random.uniform(lowVel, highVel))
        position = QPointF(x, y)

        return cls(velocity, position, h, True)

    @classmethod
    def byPosition(cls, position, h):
        mul = random.uniform(6, 9)
        randomVel = [random.uniform(-1, 1) * mul for i in range(2)]
        velocity = QPointF(randomVel[0], randomVel[1])
        position = position

        return cls(velocity, position, h, False)

"position" in "byPosition" is passed from "QPointF(0, 0)" I created two Particle objects "byPosition", and give them random velocities and compute every obeject's position in next frame.

I expected to see two different points' paths. However, the two objects share the "self.position" data. Although they have different velocities.

After I edited the "byPosition" method as followed, everything works fine. I just cannot understand a bit.

    @classmethod
    def byPosition(cls, position, h):
        mul = random.uniform(6, 9)
        randomVel = [random.uniform(-1, 1) * mul for i in range(2)]
        velocity = QPointF(randomVel[0], randomVel[1])
        position = QPointF(position.x(), position.y())

        return cls(velocity, position, h, False)

Similar question I found, @classmethod for constructor overloading

tao4free
  • 33
  • 6
  • I suspect that you are assigning something like `pos = QPointF(0, 0)`, then passing that to each invocation of `Particle.byPosition`. Please share the lines of code that invoke that method. – brentertainer Aug 05 '19 at 14:23
  • @brentertainer ```self.firework = Particle.byXY(0, 0, 255) ``` ```for i in range(2): newp = Particle.byPosition(self.firework.position, self.hue) self.particles.append(newp)```. There is a ```update``` method to move the particle according to the position and velocity. Basic ideal is one particle is moving and then split into two particles with different velocities so that they have different paths. – tao4free Aug 05 '19 at 14:40
  • 1
    `self.firework.position` is a reference to a mutable object. The issue is that you are assigning the same point object. Your change works because it creates a new object. – blues Aug 05 '19 at 14:49
  • Possible duplicate of [Changing one list unexpectedly changes another, too](https://stackoverflow.com/questions/29785084/changing-one-list-unexpectedly-changes-another-too) – blues Aug 05 '19 at 14:50
  • @tao4free Yes, thanks for sharing. You are doing as I expected, and blues's comments elaborating on the problem are correct. – brentertainer Aug 05 '19 at 14:53
  • @blues Thanks for your explanation and duplicate question link. That helps a lot. – tao4free Aug 05 '19 at 15:00
  • @brentertainer Thanks for kind commenting. I asked a similar question about value pass and reference pass about Java. Now I know they are the same question. Python is somehow easy to use, I never notice that kind of question. Nice. – tao4free Aug 05 '19 at 15:04

0 Answers0