5.3.6 Arbitrary Attributes

Reminder: Like builtin types, Cython extension types don't have a __dict__ field, so you cannot assign arbitrary attributes to them. That said, it is just a few lines to implement attribute assignment, as illustrated by the following notebook session:
{{{
%cython
cdef class X:
    cdef object __dict__
    def __init__(self):
        self.__dict__ = {}
    def __setattr__(self, attr, val):
        self.__dict__[attr] = val
    def __getattr__(self, attr):
        try:
            return self.__dict__[attr]
        except KeyError:
            raise AttributeError, "object has no attribute '%s'"%attr
///
}}}
{{{
x = X()
///
}}}
{{{
x.a = 5
///
}}}
{{{
x.a
///
5
}}}
{{{
x.b
///
Traceback (most recent call last):    x.b
  File "/Volumes/HOME/sage-stable/local/lib/python2.5/", line 1, in <module>
    
  File "/Volumes/HOME/.sage//spyx/sage20/sage20_0.pyx", line 14, in sage20_0.X.__getattr__
    raise AttributeError, "object has no attribute '%s'"%attr
AttributeError: object has no attribute 'b'
}}}

See About this document... for information on suggesting changes.