Stefan Behnel wrote:

> Neal Becker, 27.08.2010 13:32:
>> I've noticed that cython extension classes are more restrictive than
>> boost::python classes.  In particular, I believe boost::python allows
>> attributes to be added by python code to boost::python classes, while
>> cython
>> classes are 'closed'.  Is this correct?
> 
> I don't know what kind of classes boost uses, but the above only applies
> to extension types in Cython. Normal Python classes behave as in normal
> Python. And it should be possible to add a __dict__ to extension types
> without much hassle, it's just that most people don't want that.
> Initialising a dict takes time during type instantiation and takes
> additional space during the lifetime of the object.
> 
> BTW, now that you mention it - does anyone know what became of the idea to
> allow a
> 
>      cdef __dict__
> 
> attribute notation (as for __weakref__) to add a dict to an extension
> type? AFAIR, that was already proposed more than once on this list.
> 
> Stefan

I just tested it.  With boost::python you can add attribute to class as well 
as instance of class.

Example: accumulator is a boost::python extension module of mine

import pyublas
import numpy as np
from accumulator import *

s = stat2nd_double
 dir(s)
Out[20]: 
['Mean',
 'RMS',
 'Var',
 '__call__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__doc__',
 '__format__',
 '__getattribute__',
 '__hash__',
 '__iadd__',
 '__init__',
 '__instance_size__',
 '__module__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'mean',
 'rms',
 'var']

s.stuff = 'hi'

dir(s)
Out[4]: 
['Mean',
 'RMS',
 'Var',
 '__call__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__doc__',
 '__format__',
 '__getattribute__',
 '__hash__',
 '__iadd__',
 '__init__',
 '__instance_size__',
 '__module__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'count',
 'mean',
 'rms',
 'stuff',
 'var']

 x = s()

In [6]: dir(x)
Out[6]: 
['Mean',
 'RMS',
 'Var',
 '__call__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__doc__',
 '__format__',
 '__getattribute__',
 '__hash__',
 '__iadd__',
 '__init__',
 '__instance_size__',
 '__module__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'count',
 'mean',
 'rms',
 'stuff',
 'var']

In [7]: x.stuff
Out[7]: 'hi'

In [8]: x.junk='bye'

In [9]: dir(x)
Out[9]: 
['Mean',
 'RMS',
 'Var',
 '__call__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__doc__',
 '__format__',
 '__getattribute__',
 '__hash__',
 '__iadd__',
 '__init__',
 '__instance_size__',
 '__module__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'count',
 'junk',
 'mean',
 'rms',
 'stuff',
 'var']

In [10]: 

_______________________________________________
Cython-dev mailing list
Cython-dev@codespeak.net
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to