python - Extending list class to make a list of objects -


i have particle list of objects of type particle, takes 2 parameters, position , energy:

class particle(object):     def __init__(self, pos, energy):         self.x = pos         self.e = energy 

the way i've managed far create list of particles using list comprehension:

number_of_particles = 10 initial_energy = 0 particle_list = [particle(initial_energy,i) in range(number_of_particles)] 

which allows me things like:

particle_list[0].x 

which want.

however, really, follows:

particle_list = particlelist(no_of_particles, initial_energy) 

and create exact same list.

i assume have extend list class somehow i'm @ loss how this.

why not build function you. simple like:

def particlelist(no_of_particles, initial_energy):     return [particle(initial_energy,i) in range(number_of_particles)] 

this should simple way of getting list.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -