1

I'm kind of newbie as programmer, but I wish to master Python and I'm developing open source application. This application has function to gather some information. This function takes 1 parameter. This parameter can be 0, 1 or 2. 0 = False, 1 = True, 2 = Multi. Also I have an if statement that does 2 actions. 1st - (when False) gathers single type value, 2nd - (when True) gathers multiple type values and when parameter is 2 (multi) then it will gather single type (1st) and multiple types (2nd). My if statement looks like this:

if False:
    get_single_type = code.of.action
    generators.generate_data(False, get_single_type)
elif True:
    get_multiple_type = code.of.action
    generators.generate_data(True, get_multiple_type)
else:
    get_single_type = code.of.action
    generators.generate_data(False, get_single_type)
    get_multiple_type = code.of.action
    generators.generate_data(True, get_multiple_type)

Is there maybe better way of avoiding this kind of coding, like in last else statement when I call both single and multiple.

Thank you in advance.

2
  • If that is literally your code, then only the second branch will ever be executed. Commented Jan 9, 2018 at 16:57
  • @mkrieger1 read the text along with the code, he explains it Commented Jan 9, 2018 at 17:00

2 Answers 2

3

One thing I learned from Python is that although it lacks the Switch operator, you can use dictionary in a similar fashion to get things done since everything is an object:

def get_single():
    # define your single function
    get_single_type = code.of.action
    generators.generate_data(False, get_single_type)


def get_multi():
    # define your multi function
    get_multiple_type = code.of.action
    generators.generate_data(True, get_multiple_type)

actions = {
    0: [get_single],
    1: [get_multi],
    2: [get_single, get_multi]
}

parameter = 0 # replace this line with however you are capturing the parameter
for action in actions[parameter]:
    action()

This way you avoid c+p your code everywhere and have it referenced from the function, and your "actions" dictionary define the function to be used based on the parameter given.

In this case since you have multiple functions you want to call, I kept all dictionary items as a list so the structure is consistent and it can be iterated through to perform any number of actions.

Ensure you use leave out the () in the dictionary so that the functions aren't instantiated when the dictionary is defined. And remember to add () when you are actually calling the function from the dictionary to instantiate it.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, this answer will help me further in project that I'm doing. This is very helpful. I even think that this will solve more problems that I will face later on. Thank you for the input!
You're welcome. I was in the same shoes couple months ago and learned this on SO as well. It's a great way to handle switches.
Tested this way and it works perfect and the main executable code looks very beautiful as you need to read what the main executable does. Thank you again!
3

This is something you will often encounter and it is pretty much always bad practice to be repeating code. Anyway, the way to do this is use two if-statements. This way, even if the first case passes, the second case can still pass. Oh, and assuming your variable that can be 0, 1 or 2 is called x, then we could either use or and two checks:

if x == 0 or x == 2:

but, personally, I prefer using in on a tuple:

if x in (0, 2):
    get_single_type = code.of.action
    generators.generate_data(False, get_single_type)
if x in (1, 2):
    get_multiple_type = code.of.action
    generators.generate_data(True, get_multiple_type)

3 Comments

You should explicitly use the correct comparison operations x == 0 or x == 2 to avoid letting OP fall in the trap of using x == 0 or 2 which is always true.
@mkrieger1 fair enough, but we don't know what variable they are working with, but I will add x if you think that that will help
Thank you @JoeIddon this is supper, I like your explanation, but as I see further in my open source tool, the "Idlehands" answer will survey more in future. Thank you for you'r input! This answer also will help me later on in other actions that I will face.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.