add position as class attribute
This commit is contained in:
parent
35d41e1147
commit
f333013abf
1 changed files with 6 additions and 5 deletions
|
@ -52,17 +52,18 @@
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"class Particle:\n",
|
"class Particle:\n",
|
||||||
" def __init__(self, mass, velocity):\n",
|
" def __init__(self, mass, velocity, position):\n",
|
||||||
" self.mass = mass\n",
|
" self.mass = mass\n",
|
||||||
" self.velocity = velocity\n",
|
" self.velocity = velocity\n",
|
||||||
|
" self.position = position\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def momentum(self):\n",
|
" def momentum(self):\n",
|
||||||
" return self.mass * self.velocity\n",
|
" return self.mass * self.velocity\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def update_position(self, position, dt):\n",
|
" def update_position(self, dt):\n",
|
||||||
" return position + self.velocity * dt\n",
|
" return self.position + self.velocity * dt\n",
|
||||||
"\n",
|
"\n",
|
||||||
"particle = Particle(mass=2.1, velocity=0.8)\n",
|
"particle = Particle(mass=2.1, velocity=0.8, position = 8.2)\n",
|
||||||
"print(particle.momentum())"
|
"print(particle.momentum())"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -80,7 +81,7 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"position = 8.2\n",
|
"\n",
|
||||||
"new_position = particle.update_position(position, dt=0.1)\n",
|
"new_position = particle.update_position(position, dt=0.1)\n",
|
||||||
"print(new_position)"
|
"print(new_position)"
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue