You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Python wrappers require in some cases an additional wrapping to return the appropriate Python type. This can be achieved in multiple ways, but should be consistent throughout.
For example the FunctionSpace_float32/float64.element function returns a FiniteElement_float32/float64, in the Python API however these types are not exposed directly but only the corresponding wrappers FunctionSpace and FiniteElement. This means that at some point of the (wrapped) FunctionSpace.element call we need to switch to the wrapped FiniteElement type.
The question is then, how this should be (consistently) facilitated across the Python layer, see also the discussion at #3542 (comment). Options are
Python wrapper maintains a (wrapped) member variable that is exposed through the API:
Single time construction of wrapper (during construction of super class).
Requires additional state that needs to be managed/maintained - updates in cpp layer need to be propagated.
Use of cached_property to maintain a single instance without explicit management.
Resolves the problem of memory addresses of 2.
Property accesses imply lookup - cached_property documentation states it should be used for "Useful for expensive computed properties of instances that are otherwise effectively immutable." - Python wrappers are however always lightweight and how correct is the immutable assumption?
Currently used in dolfinx.fem.FunctionSpace
To resolve this one needs to decide on a consistent style, adapt it throughout and maybe document it in the developer resources
The text was updated successfully, but these errors were encountered:
schnellerhase
changed the title
Consistent handling of member variables in Python wrappers
Consistent handling of properties in Python wrappers
Dec 8, 2024
The Python wrappers require in some cases an additional wrapping to return the appropriate Python type. This can be achieved in multiple ways, but should be consistent throughout.
For example the
FunctionSpace_float32/float64.element
function returns aFiniteElement_float32/float64
, in the Python API however these types are not exposed directly but only the corresponding wrappersFunctionSpace
andFiniteElement
. This means that at some point of the (wrapped)FunctionSpace.element
call we need to switch to the wrappedFiniteElement
type.The question is then, how this should be (consistently) facilitated across the Python layer, see also the discussion at #3542 (comment). Options are
Python wrapper maintains a (wrapped) member variable that is exposed through the API:
dolfinx.mesh.Mesh
Construct wrapped object during access
FiniteElement
python wrapper #3542 (comment)Use of
cached_property
to maintain a single instance without explicit management.cached_property
documentation states it should be used for "Useful for expensive computed properties of instances that are otherwise effectively immutable." - Python wrappers are however always lightweight and how correct is the immutable assumption?dolfinx.fem.FunctionSpace
To resolve this one needs to decide on a consistent style, adapt it throughout and maybe document it in the developer resources
The text was updated successfully, but these errors were encountered: