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]:
3.0

In [9]:
cy_particle.get_momentum()


Out[9]:
3.0

In [10]:
py_particle.mass


Out[10]:
1.0

In [11]:
cy_particle.mass


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-a9e7785817af> in <module>()
----> 1 cy_particle.mass

AttributeError: '_cython_magic_4bf2e919ad94f6d2445c923510fe4893.cPa' object has no attribute 'mass'

In [12]:
py_particle.charge = 1

In [14]:
py_particle.__dict__


Out[14]:
{'charge': 1, 'mass': 1.0, 'position': 2.0, 'velocity': 3.0}

In [15]:
cy_particle.charge = 1


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-15-2be780edcd30> in <module>()
----> 1 cy_particle.charge = 1

AttributeError: '_cython_magic_4bf2e919ad94f6d2445c923510fe4893.cParticle' object has no attribute 'charge'