So I have a bunch of particles which can be of two kinds let’s say "up" and "down". I am new to python but I know that this segregation can be done with the help of a class.
Let’s say I have a list:
T=np.linspace(0,1,1000)
I want each element of this list to be either one of the two particles but I am not sure if I should create two classes with one "kind" each or a single class with two "instances" and assign each element an instance.
In the end, what I want to do is randomly distribute this property of being "up" and "down" over the list T.
>Solution :
There is not enough info. You most likely don’t want 2 instances when dealing with 1000 items – each item should be probably represented by separate instance of a class. But that doesn’t mean that having 2 classes is a way to go.
Are the particles relatively similiar and besides the difference of a single property they have the same behaviour? If so, I’d go for single class, 1000 instances and storing the up/down property in some instance attribute.
EDIT:
This is how I would personally implement this with for the current requirements in question:
class Particle:
def __init__(self, pos, mom, spin):
self.pos = pos
self.mom = mom
self.spin = spin
T = [Particle(pos = i, mom = 0, spin = random.choice(["up", "down"])) for i in np.linspace(0, 1, 1000)]
print([t.spin for t in T[:10]])