Initialization of Python attributes of extension types

12 views
Skip to first unread message

Jeroen Demeyer

unread,
Dec 17, 2017, 1:55:30 PM12/17/17
to cython-users
Say I have some extension type like

cdef class X:
cdef object attr

When I create a new instance of X (using either X.__new__(X) or X()),
will the "attr" attribute be initialized to None? The documentation says

"Any Python attributes have also been initialised to None, but you
probably shouldn’t rely on that."

That piece of documentation was written in 2009, so I wonder whether it
is still up to date. Can we assume that Python attributes of extension
type instances are initialized to None?


Jeroen.

Stefan Behnel

unread,
Dec 17, 2017, 2:11:59 PM12/17/17
to cython...@googlegroups.com
As long as Cython controls the type hierarchy, you can rely on it. Exposing
NULL values for Python attributes to Python/Cython code would be unhelpful.

There is one exception: when "__cinit__()" is called, attributes that are
defined by *subtypes* have not been initialised yet and thus are still NULL
instead of None.

Stefan

Jeroen Demeyer

unread,
Dec 18, 2017, 4:07:08 AM12/18/17
to cython...@googlegroups.com
On 2017-12-17 20:11, Stefan Behnel wrote:
> As long as Cython controls the type hierarchy

What do you mean with this? You mean that things might go wrong for a
Python subclass of a Cython extension type?
Reply all
Reply to author
Forward
0 new messages