Page 1 of 2
Writing a function to invert a boolean?
Posted: Wed Apr 27, 2022 3:57 pm
by ardthusiast
Hi,
I need to write a function that inverts the value of a specified boolean, so if its value is True it becomes False, and vice versa. I can't seem to find any great ways to do this in Python other than doing something like
which would mean that I have to write a function for every boolean that I want to invert. I don't want to do that, obviously. Any ideas? I know C# has a ref keyword that allows you to pass a variable by reference rather than by value, but Python doesn't seem to have anything similar.
Re: Writing a function to invert a boolean?
Posted: Wed Apr 27, 2022 4:34 pm
by stijn
The function which inverts a boolean is just
since it's Python. There shouldn't be any good reasons why you cannot use it. What are you really trying to achieve?
Re: Writing a function to invert a boolean?
Posted: Wed Apr 27, 2022 4:39 pm
by karfas
What exactly is your use case for this ?
I don't think I have had any use for such a function during my ~40 years in software development. Ever.
And I used mostly pass-by-value languages.
Re: Writing a function to invert a boolean?
Posted: Wed Apr 27, 2022 5:06 pm
by ardthusiast
It's for an event handler. Basically, when the event handler is called, I want it to invert the value of a boolean. The event handler can only execute functions, so I cannot use the not operator.
Re: Writing a function to invert a boolean?
Posted: Wed Apr 27, 2022 5:12 pm
by ardthusiast
Maybe Micropython has a way to modify the value of a variable at a certain memory address? So instead of passing the variable itself, I could pass the variable's memory address which would then allow me to directly modify the variable from inside the function?
Re: Writing a function to invert a boolean?
Posted: Wed Apr 27, 2022 6:26 pm
by pythoncoder
Would storing the booleans in an array or list help? The function could then invert any element given the index.
Re: Writing a function to invert a boolean?
Posted: Wed Apr 27, 2022 6:49 pm
by stijn
ardthusiast wrote: ↑Wed Apr 27, 2022 5:06 pm
The event handler can only execute functions, so I cannot use the not operator.
Can you elaborate? Under the hood operators are implemented almost just like function calls.
Anyway if you really cannot use 'not' nor operators then things become tricky. No operators would mean no +, no == etc?
I could pass the variable's memory address which would then allow me to directly modify the variable from inside the function?
That doesn't help if you cannot use 'not'.
All in all, still not clear why exactly you cannot do 'x = not x' (where x could also be an array/memoryview/... element i.e. a view on a piece of memory).
Re: Writing a function to invert a boolean?
Posted: Wed Apr 27, 2022 8:41 pm
by karfas
ardthusiast wrote: ↑Wed Apr 27, 2022 5:06 pm
It's for an event handler. Basically, when the event handler is called, I want it to invert the value of a boolean. The event handler can only execute functions, so I cannot use the not operator.
I still don't get it. Maybe you can illustrate the problem with a few lines of code ?
When the event handler wants a function, you will need to provide that function.
Even a (hypothetical) generic invert() call operating via a reference or pointer will need a parameter (that you most likely can't provide, as the interface to the event handler is a function!).
Re: Writing a function to invert a boolean?
Posted: Wed Apr 27, 2022 8:43 pm
by karfas
karfas wrote: ↑Wed Apr 27, 2022 8:41 pm
ardthusiast wrote: ↑Wed Apr 27, 2022 5:06 pm
It's for an event handler. Basically, when the event handler is called, I want it to invert the value of a boolean. The event handler can only execute functions, so I cannot use the not operator.
I still don't get it. Maybe you can illustrate the problem with a few lines of code ?
When the event handler wants a function, you will need to provide that function.
Even a (hypothetical) generic invert() call operating via a reference or pointer will need a parameter (that you most likely can't provide, as the "thing" called by the event handler is a function!).
Re: Writing a function to invert a boolean?
Posted: Thu Apr 28, 2022 12:22 am
by ardthusiast
pythoncoder wrote: ↑Wed Apr 27, 2022 6:26 pm
Would storing the booleans in an array or list help? The function could then invert any element given the index.
Hmm, that would probably work. I'll give it a try.