Python Forum
two conditionals with intermediate code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: two conditionals with intermediate code (/thread-28267.html)



two conditionals with intermediate code - Skaperen - Jul-11-2020

i encounter this case a lot. in an elif sequence of code i need to perform 2 tests at one place. that often means an and or or between 2 tests. sometimes the 2nd test needs to run some code to produce what is to be tested. i usually have to put that code before the first if. sometimes i can do the test in a function. in C i could store the value to be tested if it was part of the 1st test. i wonder if anyone has worked out a general way of this that is Pythonic.


RE: two conditionals with intermediate code - bowlofred - Jul-11-2020

I'm not sure I'm understanding your description. Could you give an example in C of how you would do it?


RE: two conditionals with intermediate code - Skaperen - Jul-12-2020

i don't have any code handy that i can show. i'll try to describe something simple. a one place in the elif sequence i want to test a string for an '=' character. if it does have an '=' in it, i also want to test for the part before the '=' being in a dictionary. so it would look like
elif '=' in my_string and my_string.split('=',1)[0] in magic_keys:
that works but i also want the part after the '=', so
key,value = mystring.split('=',1)
is going to be done, somewhere. i'd like to do the split only one time. in C it is easy to save results and test them. so i end up making code that does things twice.

if the compiler phase optimized well, it could see that split is done twice with like args and make the run time p-code that does the split just once. then i would not worry about it. gcc has very good optimization of cases like that. but i really don't know what i get out of CPython.


RE: two conditionals with intermediate code - bowlofred - Jul-12-2020

In this specific case I don't see any downside to just doing the split initially. You can find out if the '=' is present from the result of the split.

l = ["no equals", "this=that"]

for s in l:
    items = s.split('=', maxsplit=1)
    if len(items) > 1:
        # has the equals
        print(f"{items[0]}  ->  {items[1]}")
    else:
        print(f"'{items[0]}' didn't have an equals sign")
And if you wanted to, you could use the walrus operator to combine the split and the if lines.

If split behaved differently than it does and failed when the split character was not present, then I'd probably wrap it in a try/except. I'm not sure if that's useful, or if I've misinterpreted something.


RE: two conditionals with intermediate code - Gribouillis - Jul-12-2020

Python is high level, so I suggest a function

elif is_magic_key_assignment(my_string):



RE: two conditionals with intermediate code - Skaperen - Jul-12-2020

a big reason i don't like to use functions for things like this is the context change. if there was a way to have a kind of mini-function that runs in the same context as the caller, that would make this easier. otherwise, argument lists can get long.