Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A class for spin up and down particles?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading