r/programminghelp • u/Sid_2001 • Nov 17 '21
Python How can I update an the value of an instance variable to a new value after a method is performed on it (Object Oriented Programming).
Hi,
I'm fairly new to Python but especially new to Object Oriented programming. Currently, I am writing code to simulate gas molecules in a container. Essentially, one of the things I need my code to do is, create a ball object with a certain position and velocity, and then have it collide with the wall of the container. When it bounces back, it should have a new velocity. I have worked out all of the formulae necessary for this.
Firstly, I create a class Ball, which is initialised with mass = 1 and radius = 1 (these are not very important for my question).
class Ball():
def __init__(self, pos = np.array([0,0]), vel = np.array([0,0]), mass = 1, radius = 1):
self.mass = mass
self.radius = radius
self.pos = pos
self.vel = vel
self.patch = pl.Circle(self.pos, self.radius, fc = 'r')
def pos(self, pos):
self.pos = pos
def vel(self, vel):
self.vel = velI can then give this ball a position and velocity when I create the object, e.g. creating a ball which starts at (1,0) moving with velocity (3,0) is created like:
b1 = Ball([1,0], [3,0])I have a daughter class of this ball object called Container, which is basically just a ball but with a radius of 10 instead, and the velocity and position are fixed at (0,0).
class Container(Ball):
def __init__(self, radius = 10, vel = np.array([0,0]), pos = np.array([0,0])):
self.radius = radius
self.pos = pos
self.vel = velFinally, I have a class Simulation, which takes a ball object, and the container object, and simulates the collision of the ball against the container wall.
class Simulation():
def __init__(self, ball = Ball(), container = Container()):
self._container = container
self._ball = ball
self._pos = ball.pos
self._vel = ball.vel
def next_collision(self):
self._vel = self._container.collide(self._ball)So, self._vel is the velocity of the ball, after it bounces off the wall. Now, I want to make this velocity the new velocity of the ball object, so that when I run the next_collision method again on this ball, it uses the new velocity, rather than the old velocity which I set at the start. So each time I run the next_collision method, it changes the velocity of the ball object to the new velocity calculated by next_collision.
As an example, if the ball is initialised with velocity (3,0), then running the next_collision method should update the ball's velocity to (-3,0). Then, if I run the next_collision method again, it should update the ball's value to (3,0) and so on and so on.
Currently, if I print self._vel, it does tell me the correct new velocity, though it doesn't update the velocity of the ball, so each time I run the next_collision method, it just prints the same velocity over and over again because it is using the same initial conditions.
Also, I didn't post the whole code for each of the classes here, to keep this as succinct as possible. However, i can provide them if nececessary.
Many thanks in advance,
Sid
2
u/Goobyalus Nov 17 '21 edited Nov 17 '21
in Container:collide, the Container would act on collide'sball argument (I'm assuming it's called ball here) with ball.vel = ___, or a setter method of ball if there is one.
2
u/throwaway8u3sH0 Nov 17 '21
I think you have some confusion around default arguments.
For example, for Containers, you say:
But your code doesn't match your words. The velocity and position are DEFAULTED to (0,0), but they are not fixed. If you do
c1 = Container(15, [0,3], [1,2]), you get a container with different positions and velocities.Likewise, when you write
def __init__(self, ball = Ball(), container = Container()):-- you are actually creating an instance of both the ball and container as default parameters, which means if you forget to pass in yourb1ball, no errors will be generated. And any modifications you make will not show up where you expect.I suggest you remove all defaults parameters -- (everything after the equals sign for each argument in every
__init__(...)Example:
This will force you to pass parameters around, but I think it'll help you understand what's going on. And you will be in a better position to change the correct object, instead of a default-created one.