Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent handling of properties in Python wrappers #3553

Open
schnellerhase opened this issue Dec 8, 2024 · 0 comments
Open

Consistent handling of properties in Python wrappers #3553

schnellerhase opened this issue Dec 8, 2024 · 0 comments

Comments

@schnellerhase
Copy link
Contributor

schnellerhase commented 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 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

  1. 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.
    • Currently used for example in dolfinx.mesh.Mesh
  2. Construct wrapped object during access

  3. 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

@schnellerhase schnellerhase changed the title Consistent handling of member variables in Python wrappers Consistent handling of properties in Python wrappers Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant