Closed heuer closed 5 months ago
A lot of methods need to check if the input is None and use either the _ptr attribute of the input or call the C function with ffi.NULL:
None
_ptr
ffi.NULL
def method(self, obj: SomePtrType | None) -> None: if obj is None: lib.something(self._ptr, ffi.NULL) else: lib.something(self._ptr, obj._ptr)
Having a helper function which either returns the _ptr attribute or ffi.NULL the code could be reduced to:
def method(self, obj: SomePtrType | None) -> None: obj_ptr = ptr_or_null(obj) lib.something(self._ptr, obj_ptr)
IMO it is a lot easier to read and to type.
Could be reduced to a one-liner, though:
def method(self, obj: SomePtrType | None) -> None: lib.something(self._ptr, ptr_or_null(obj))
A lot of methods need to check if the input is
None
and use either the_ptr
attribute of the input or call the C function withffi.NULL
:Having a helper function which either returns the
_ptr
attribute orffi.NULL
the code could be reduced to:IMO it is a lot easier to read and to type.
Could be reduced to a one-liner, though: