[Cython] CEP 507/513
Martin C. Martin
martin at martincmartin.com
Sun Apr 13 17:34:46 CEST 2008
Dag Sverre Seljebotn wrote:
> Martin C. Martin wrote:
>> I too would be worried about changing the semantics of cdef T a = x.
>> But what about:
>>
>> cdef T a = x
>> assert a.__class__ == T
>>
>> This makes it valid Python, which pins down the type of "a" exactly.
>> In fact, at this point you don't even need the "cdef T".
>>
>> It's slightly ugly, but with luck, that will discourage people from
>> premature optimization.
> It's still dangerous:
>
> a.myfunc = myoverride
> assert a.__class__ == T
> a.myfunc()
True. It would be good to have a way to turn off the per-instance
metaclass for instances of a given type, i.e. not allow functions in the
object's __dict__. It wouldn't be that hard, in the constructor you
could replace the object's __dict__ with a subclass of __dict__ that
overrides the setter.
But then you have the same problem with the class, namely
a = T()
T.myfunc = myoverride
a.myfunc() # Calls myoverride.
Not sure how you'd disable it in the class.
> (On the subject of optimizations, one could also augment the CEP by
> having the notation "exactly(MyClass)" mean "early-bind all calls and I
> don't care about the consequences". Using "exactly(list)" would then
> mean that builtins wouldn't need to be a special-case either. But I'm
> fine with "list" being a special-case here.)
Well, special cases make things very confusing for the programmer, so
I'm against them when possible.
Best,
Martikn
More information about the Cython-dev
mailing list