, , __getitem__ . , , . __getitem__ __setitem__ , , , :
p= Particle(foo)
bar = p[0]
, .
, . , , , __init__, . self:
class Particle:
def __init__(self, mass, position, velocity, force):
self.mass = mass
self.position = position
self.velocity = velocity
self.force = force
. , :
mass,pos,vel,f = 0,0,0,0
p = Particle(mass,pos,vel,f)
print p.mass, p.position, p.velocity, p.force
, , , python , p, , Particle, :
in [1]: p
out[1]: <__main__.Particle instance at 0x03E1fE68>
, , , " ", . (, __getitem__), . , .
, , , , :
in [2]: p.mass
out[2]: 0
in [3]: p.mass = 2
in [4]: p.mass
out[4]: 2
, , __init__ class ( / ). , , . , , , , . , , , .
in [5]: p.newattr ='foobar!'
in [6]: p.newattr
out[6]: 'foobar!'
? ... , , . , , , . , .