Reputation: 4970
This is in micropython
I'm creating an API to control some hardware. The API will be implemented in C with an interface in micropython.
One example of my API is:
device.set(curr_chan.BipolarRange, curr_chan.BipolarRange.state.ON)
I'd like to be able to achieve the same functionality but shorten the second path by somehow implicitly referencing the first argument:
device.set(curr_chan.BipolarRange, <first arg?>.state.ON)
Is there anyway to do this?
The only way to do something like this now would be
device.set(curr_chan.BipolarRange.state.ON)
and then put an upward pointing C-pointer on both the ON
C-object and state
C-object so that I know which entry in curr_chan
is being referenced.
The micropython runtime - and I assume CPython one - doesn't keep the entire object "tree" available to the developer in memory.
Upvotes: 1
Views: 198
Reputation: 114330
Pass the name of the attribute you want as the second argument. Call getattr
(or PObject_GetAttr
repeatedly to get each element of the .
-separated string:
device.set(curr_chan.BipolarRange, 'state.ON')
Upvotes: 1
Reputation: 33719
You could have special values for the second (state) argument which tell the function implementation to derive the state from the first argument. You could also introduce a completely separate function which has this behavior.
Or you could have a helper function which determines the state and passes it down to the set
function, something like this:
device.set(*state_ON(curr_chan.BipolarRange))
Here, state_ON
would return a tuple (curr_chan.BipolarRange, curr_chan.BipolarRange.state.ON)
.
In any case, there is no direct support for what you are trying to do in Python itself.
Upvotes: 1