Skip to content

dontmindmehere/python-if_statements

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

python-if_statements

support for control flow in python

There are two main ways to implement If/Elif/Else statements in python:

    
    from if_statements import If, Elif, Else
    

1.Using a variable:

a. Initialize an instance of If, bind it to a variable, and give it a condition as well as an action (which is a function, followed by any number of arguments).

    
    Basic instantiation:
        my_if = If(i == 0)
        my_if.do(print, "i is 0", F"{i = }")
    
    One liner:
        my_if = If(i == 0).do(print, "i is 0", F"{i = }")
    
    For clarity:
        my_if = If(i == 0).do(
            print, "i is 0", F"{i = }"
            )
    

b. Attach any number of Elif instances to it, and a single instace of Else. Remember that Else does not have any conditions.

    
    Elif(my_if, i == 1).do(
        print, "i is 1", F"{i = }"
    )
    Elif(my_if, i == 2).do(
        print, "i is 2", F"{i = }"
    )
    Else(my_if).do(
        print, "i is not 0, 1, or 2", F"{i = }"
    )
    

2.Chaining method access:

a.In this case, it's really easy to instantiate If. Initialization and declaration of all of its Elif objecs and its instance of Else are done in a single command. There's also no need to bind your instance of If to a variable, nor to pass it to its subsequent children.

The same example from the first method would be written like this:

    
    If(i == 0).do(
        print, "i is 0", F"{i = }"
    ).Elif(i == 1).do(
        print, "i is 1", F"{i = }"
    ).Elif(i == 2).do(
        print, "i is 2", F"{i = }"
    ).Else().do(
        print, "i is not 0, 1, or 2", F"{i = }"
    )
    

You can also make the code clearer by inserting a line break after the conditional instantiation.

    
    If(i == 0
        ).do(
        print, "i is 0", F"{i = }"
    ).Elif(i == 1
        ).do(
        print, "i is 1", F"{i = }"
    ).Elif(i == 2
        ).do(
        print, "i is 2", F"{i = }"
    ).Else(
        ).do(
        print, "i is not 0, 1, or 2", F"{i = }"
    )
    

About

(humor) support for if/elif/else statements in python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages