[Pyrex] [Cython] Proposed change to behaviour of "not None"
Dag Sverre Seljebotn
dagss at student.matnat.uio.no
Thu Aug 21 13:14:35 CEST 2008
Greg Ewing wrote:
> Lisandro Dalcin wrote:
>
>> I know, I can (and I do) use 'Type arg is not
>> None' in argument lists, but I never liked it.
>
> Speaking of that, I've been thinking for a while now
> about making "not None" the default, and requiring
> "or None" to specify that None is an acceptable
> parameter value.
>
> Having seen a number of pieces of Pyrex code that
> were susceptible to being crashed because "not None"
> wasn't used where it should have been, I think
> this would be a safer default. The change may
> break some existing code, though.
>
> What do people think?
>
For Cython at least, I'm -1 on this.
The reason is that it breaks backwards compatability (although
admittedly in a nice way) while there is another kind of behaviour I
would prefer to move to: That None is allowed, but is checked for so
that it is safe.
The trivial solution might slow things down a lot, but going beyong the
trivial isn't horribly difficult either by using a visitor [1].
What I'd like to see in addition then is some kind of type specifier
saying that a variable cannot be None which could also be used for
variables.
cdef MyClass not None var
is obviously problematic, but that's what I'd like just with a better
syntax. Something like
cdef cython.notnone(MyClass) var
cdef notnone MyClass var
cdef MyClass! var
or something along these lines. The effect of this would be to have the
check happen at variable assignment rather than attribute lookup.
Finally, a compiler directive could be set to assume that no
AttributeErrors (with respect to None) occurs to remove these checks,
but it would not be the default.
I know, I have lots of plans that I can never get to the bottom of, but
at least I feel the existance of this solution means one should at least
wait with changing the "not None" behaviour for Cython.
[1] Like just checking if you're within an if-test where the local
variable was checked to not be None + only the first attribute lookup on
a local variable in the same block needs a check, because an exception
drops you out of it. These two rules gets you a long way.
--
Dag Sverre
More information about the Pyrex
mailing list