Writing a function to invert a boolean?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
ardthusiast
Posts: 25
Joined: Tue Feb 08, 2022 4:13 pm

Writing a function to invert a boolean?

Post by ardthusiast » Wed Apr 27, 2022 3:57 pm

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

Code: Select all

boolean = not boolean
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.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Writing a function to invert a boolean?

Post by stijn » Wed Apr 27, 2022 4:34 pm

The function which inverts a boolean is just

Code: Select all

not
since it's Python. There shouldn't be any good reasons why you cannot use it. What are you really trying to achieve?

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Writing a function to invert a boolean?

Post by karfas » Wed Apr 27, 2022 4:39 pm

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.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

User avatar
ardthusiast
Posts: 25
Joined: Tue Feb 08, 2022 4:13 pm

Re: Writing a function to invert a boolean?

Post by ardthusiast » 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.

User avatar
ardthusiast
Posts: 25
Joined: Tue Feb 08, 2022 4:13 pm

Re: Writing a function to invert a boolean?

Post by ardthusiast » Wed Apr 27, 2022 5:12 pm

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?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Writing a function to invert a boolean?

Post by pythoncoder » 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.
Peter Hinch
Index to my micropython libraries.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Writing a function to invert a boolean?

Post by stijn » Wed Apr 27, 2022 6:49 pm

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

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Writing a function to invert a boolean?

Post by karfas » 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 interface to the event handler is a function!).
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Writing a function to invert a boolean?

Post by karfas » Wed Apr 27, 2022 8:43 pm

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!).
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

User avatar
ardthusiast
Posts: 25
Joined: Tue Feb 08, 2022 4:13 pm

Re: Writing a function to invert a boolean?

Post by ardthusiast » Thu Apr 28, 2022 12:22 am

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.

Post Reply