Reputation: 5087
I'm trying to grant python access for a member inside a cython class. The member type is basic (e.g. int
or float
)
As I read in the documentation, you can use properties to give access to the underlying C++ member:
cdef class myPythonClass:
# grant access to myCppMember thanks to myMember
property myMember:
def __get__(self):
return self.thisptr.myCppMember # implicit conversion
# would somehow be the same logic for __set__ method
Now this works.
However, as far as I understand, for basic types you can just use extension types. In this case, you make the member public
to make it accessible and/or writable. You don't need properties:
cdef class myPythonClass:
cdef public int myCppMember # direct access to myCppMember
But when I use this second option, it does not work. The variable is never updated. Is there something I'm missing or I did not fully understood?
Thanks for you input.
Upvotes: 1
Views: 744
Reputation: 59005
You already found the solution, using property
is the way to go.
A public
attribute can be accessed outside a class method, while a private
attributes can only be used by the class methods.
But even public
attributes defined at the C++ level cannot be accessed from Python. And exposing either a private
or a public
attribute using property
will make it available to Python.
Upvotes: 1