In [1]:
%load_ext Cython
In [2]:
# Pure Python version
class Particle(object):
"""Simple Particle type."""
def __init__(self, m, x, v):
self.mass = m
self.position = x
self.velocity = v
def get_momentum(self):
return self.mass * self.velocity
In [3]:
%%cython
cdef class cParticle(object):
"""Simple Particle type."""
cdef double mass, position, velocity
def __init__(self, m, x, v):
self.mass = m
self.position = x
self.velocity = v
def get_momentum(self):
return self.mass * self.velocity
In [4]:
Particle?
In [5]:
cParticle?
In [6]:
py_particle = Particle(1.0, 2.0, 3.0)
In [7]:
cy_particle = cParticle(1.0, 2.0, 3.0)
In [8]:
py_particle.get_momentum()
Out[8]:
In [9]:
cy_particle.get_momentum()
Out[9]:
In [10]:
py_particle.mass
Out[10]:
In [11]:
cy_particle.mass
In [12]:
py_particle.charge = 1
In [14]:
py_particle.__dict__
Out[14]:
In [15]:
cy_particle.charge = 1