[SciPy-User] Mapping objects to numpy array
Chris Colbert
sccolbert@gmail....
Wed Aug 5 15:32:43 CDT 2009
You cannot use self.a, as this creates an instance attribute that
hides the class attribute. You have to access through the __class__
variable of the instance, or access the class attribute directly. See
below:
In [7]: class Test(object):
...: a = 1
...: def __init__(self, val):
...: self.b = val
...:
...: def set(self, val):
...: self.a = val
...:
...:
In [8]: t1 = Test(4)
In [9]: t1.a
Out[9]: 1
In [10]: t1.b
Out[10]: 4
In [11]: t2 = Test(5)
In [12]: t1.a
Out[12]: 1
In [13]: t2.a
Out[13]: 1
In [14]: t2.b
Out[14]: 5
In [15]: t1.set(4)
In [16]: t1.a
Out[16]: 4
In [17]: t1.b
Out[17]: 4
In [18]: t2.a
Out[18]: 1
In [19]: t2.b
Out[19]: 5
In [20]: t3 = Test(6)
In [21]: t4 = Test(7)
In [22]: t3.a
Out[22]: 1
In [23]: t4.a
Out[23]: 1
In [24]: Test.a = 9
In [25]: t3.a
Out[25]: 9
In [26]: t4.a
Out[26]: 9
On Wed, Aug 5, 2009 at 11:55 AM, <josef.pktd@gmail.com> wrote:
> On Wed, Aug 5, 2009 at 11:32 AM, Jim Vickroy<Jim.Vickroy@noaa.gov> wrote:
>> josef.pktd@gmail.com wrote:
>>
>> On Wed, Aug 5, 2009 at 9:27 AM, Chris Colbert<sccolbert@gmail.com> wrote:
>>
>>
>> its for the same reason you cant do this:
>>
>> In [1]: a = 1
>>
>> In [2]: b = a
>>
>> In [3]: a
>> Out[3]: 1
>>
>> In [4]: b
>> Out[4]: 1
>>
>> In [5]: a = 2
>>
>> In [6]: a
>> Out[6]: 2
>>
>> In [7]: b
>> Out[7]: 1
>>
>>
>> but you CAN do this:
>>
>> In [8]: a = [1, 2]
>>
>> In [9]: b = a
>>
>> In [10]: a
>> Out[10]: [1, 2]
>>
>> In [11]: b
>> Out[11]: [1, 2]
>>
>> In [12]: a[0] = 0
>>
>> In [13]: a
>> Out[13]: [0, 2]
>>
>> In [14]: b
>> Out[14]: [0, 2]
>>
>>
>> floats and ints are immutables in python.
>> you'll need a mutable container to store the values how you are wanting.
>>
>> That said, you still cant map to them with a numpy array like you want
>> (AFAIK)
>>
>> You may want to look into subclassing ndarray or reformulating your
>> problem so you dont need this requirement.
>>
>>
>> On Wed, Aug 5, 2009 at 9:05 AM, Emmanuelle
>> Gouillart<emmanuelle.gouillart@normalesup.org> wrote:
>>
>>
>> Hi Jack,
>>
>> I don't think you can do it this way. The reason is that p1.x and
>> p1.v can be anywhere in the memory, maybe at very different places. So
>> np.array makes a *copy* when you pass ( p1.x, p1.v) as arguments. I tried
>> setting copy=False as a keyword argument of np.array but it doesn't
>> change the result; apparently a copy has to be made. Why don't you define
>> an array inside the class Particle, instead of different attributes?
>>
>> Cheers,
>>
>> Emmanuelle
>>
>> On Wed, Aug 05, 2009 at 02:44:18PM +0200, Jack Liddle wrote:
>>
>>
>> Hi
>>
>>
>> I'm trying to map some objects to a numpy array, so I can solve some
>> ode's with them and then work on the results with my objects.
>>
>>
>> Here's some code.
>>
>>
>> from numpy import array
>>
>>
>> class Particle(object):
>> def __init__(self,x,v):
>> self.x = x
>> self.v = v
>>
>>
>> p1 = Particle(1,0)
>> a = array(( p1.x, p1.v))
>> a[0] = 5
>> print p1.x
>>
>>
>> This prints 1 instead of 5. How do I make the array 'a' share the
>> same memory as the p1 attributes?
>>
>>
>> Any ideas, hope this makes sense
>>
>>
>> Thanks
>>
>>
>> Jack Liddle
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
>>
>> I was trying to do it from scratch, but I got confused about class
>> variables.
>> Can class variables only be accessed through the class self.__class__
>> ? I seem to get rusty in my python knowledge.
>>
>> the following seems to do what Emmanuelle proposed, but there might be
>> better examples already in the mailing lists.
>>
>> Josef
>>
>>
>> class Particle(object):
>> a = np.empty((3,2))
>>
>>
>
>> The above definition means that all instances of your Particle class will
>> share the same "a".
>> If you create instances p1 = Particle(0,1,2) and p2 = Particle(3,4,5), p1.a
>> and p2.a reference the same "a".
>> Is that your intent?
>
> Yes, that's the point of the exercise, to have the data of all
> particles accessible through one array, and the individual data points
> accessible through the instance attribute.
>
>>
>> def __init__(self,k,x,v):
>> self.k = k
>> #why do I need self.__class__ for class variable
>>
>>
>> self.a should work just fine. Did you try it without specifying the
>> __class__ qualifier?
>
> Thanks for the hint.
> Yes, it works, and no, I didn't try.
> I was worried about class versus instance variable, the python help
> only used the class to access the class variable, and I didn't want to
> spend too much time on the exercise.
>
> When I actually need it, I will worry about the other details, like
> how to preallocate and grow the class array `a` and to automatically
> assign indices.
>
> Josef
>
>
>>
>> self.__class__.a[k,0] = x
>> self.__class__.a[k,1] = v
>>
>> def _getx(self):
>> return self.__class__.a[self.k,0]
>> def _setx(self, value):
>> self.__class__.a[self.k,0] = value
>>
>> x = property(_getx, _setx)
>>
>>
>> aa = Particle.a
>> print aa
>> p1 = Particle(0, 1, 2)
>> print p1
>> p2 = Particle(1, 2, 3)
>> p3 = Particle(2, 3, 4)
>> print aa
>> aa[:,:] = 1
>> print aa
>> print p2._getx(), p2.x
>> aa[0,0] = 5
>> print p1.x
>> p2.x = 10
>> print aa
>> print Particle.a
>> print vars(p2)
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
More information about the SciPy-User
mailing list