Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen I access a list on an object it incorrectly returns as empty list
    primarykey
    data
    text
    <p>I am writing a code for a computational physics project.</p> <p>I'd like to know how I can implement a class in python by adding an array to the instances of the class.</p> <p>Here is my original part of code:</p> <pre><code>class Particle(): def __init__(self, (x,y,z), n, radius, neighbours): self.n = n self.x = x self.y = y self.z = z self.radius = radius number = loadtxt("final_limited.txt", usecols=(0,), unpack=True, dtype = int) c1,c2,c3,r = loadtxt("final_limited.txt", usecols=(1,2,3,5), unpack=True, dtype=float) number_of_particles = len(number) my_particles = [] overlap = [] contact_number = [] for i in range(number_of_particles): n = number[i] x = c1[i] y = c2[i] z = c3[i] radius = r[i] neighbours = [] particle = Particle((x,y,z), n, radius, neighbours) my_particles.append(particle) for particle1 in my_particles: count = 0 for particle2 in my_particles: distance = PBCdist(particle1, particle2) sum_of_radii = Sum_radii(particle1, particle2) if (distance &lt; sum_of_radii) and (distance&gt;0): count +=1 olap = (Decimal(sum_of_radii) - Decimal(distance)) overlap.append(olap) contact_number.append(count) </code></pre> <p>I would like to do the following:</p> <pre><code>class Particle(): def __init__(self, (x,y,z), n, radius, neighbours): neighbours = [] self.n = n self.x = x self.y = y self.z = z self.radius = radius self.neighbours = neighbours </code></pre> <p>And then, in the nested loop:</p> <pre><code>for particle1 in my_particles: count = 0 for particle2 in my_particles: distance = PBCdist(particle1, particle2) sum_of_radii = Sum_radii(particle1, particle2) if (distance &lt; sum_of_radii) and (distance&gt;0): count +=1 neighbours.append(particle2.n) olap = (Decimal(sum_of_radii) - Decimal(distance)) overlap.append(olap) contact_number.append(count) </code></pre> <p>As you can see, I'd like to give associate to each particle the list of its neighbours as a property of each element of the class.</p> <p>However, when I check it this code does not work. I'd like tp be able to say type:</p> <pre><code>print my_particles[0].neighbours </code></pre> <p>And obtain the list.</p> <p>Moreover, do you know if there is a Numpy dtype like float that can give me the required 20-21 decimal places? Using float data type my code is of course faster(10 times), but i'd like to use a numpy type that allows full precision required, rather than Decimal.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload