Python Forum
two conditionals with intermediate code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
two conditionals with intermediate code
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I'm not sure I'm understanding your description. Could you give an example in C of how you would do it?
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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.
Reply
#5
Python is high level, so I suggest a function

elif is_magic_key_assignment(my_string):
Reply
#6
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Doubt about conditionals in Python. Carmazum 6 1,536 Apr-01-2023, 12:01 AM
Last Post: Carmazum
  conditionals based on data frame mbrown009 1 873 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Efficiency with regard to nested conditionals or and statements Mark17 13 3,082 May-06-2022, 05:16 PM
Last Post: Mark17
  Nested conditionals vs conditionals connected by operators dboxall123 8 2,984 Feb-18-2022, 09:34 PM
Last Post: dboxall123
  Nested Conditionals shen123 3 2,584 Jul-28-2021, 08:24 AM
Last Post: Yoriz
  Invalid syntax using conditionals if - else jperezqu 1 2,295 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  conditionals with boolean logic?? ridgerunnersjw 3 1,958 Sep-26-2020, 02:13 PM
Last Post: deanhystad
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 2,925 Jun-04-2020, 12:08 PM
Last Post: Drone4four
  Nested Conditionals HELP absolum 4 2,726 Jul-17-2019, 06:40 PM
Last Post: absolum
  intermediate book recommendation please? jameson984 1 1,982 Jul-15-2019, 03:21 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020