Reuse the value of previous type instead of None I've created a function in python to print some information through it. I'm trying to supply list of alphabets to the function along with the type those alphabets fall under.

When I execute the function, It checks whether the value of the type is None. If it is None, it executes the except block to fetch one.

I've tried with (working one):

def get_info(alpha,alpha_type):

    print("checking value of item:",alpha_type)

    try:
        if not alpha_type:raise
        return alpha,alpha_type
    except Exception:
        alpha_type = "vowel"
        return get_info(alpha,alpha_type)

if __name__ == '__main__':
    main_type = None
    for elem in ["a","e","o"]:
        result = get_info(elem, alpha_type=main_type)
        main_type = result[1]
        print(result)


Output it produces (as expected):

checking value of item: None
checking value of item: vowel
('a', 'vowel')
checking value of item: vowel
('e', 'vowel')
checking value of item: vowel
('o', 'vowel')


Although the value of the type is None when an alphabet is supplied to the function, ain't there already the value of previous type (when run twice) stored in the memory. However,
When I try like the following, I get messy output:

def get_info(alpha,alpha_type):

    print("checking value of item:",main_type)

    try:
        if not alpha_type:raise
        return alpha,alpha_type
    except Exception:
        alpha_type = "vowel"
        return get_info(alpha,alpha_type)

if __name__ == '__main__':
    for elem in ["a","e","o"]:
        print(get_info(elem,alpha_type=None))


Output the second function produces:

checking value of item: None
checking value of item: vowel
('a', 'vowel')
checking value of item: None
checking value of item: vowel
('e', 'vowel')
checking value of item: None
checking value of item: vowel
('o', 'vowel')


I would like to get the expected output (what the first function produces) following the design of the second function, so that I don't need to influence the result from outside of the function.
First, your code reformatted properly so it's more legible:

def get_info(alpha, alpha_type):

    print("checking value of item:", alpha_type)

    try:
        if not alpha_type:
            raise

        return alpha, alpha_type

    except Exception:
        alpha_type = "vowel"
        return get_info(alpha, alpha_type)


if __name__ == '__main__':
    main_type = None
    for elem in ["a", "e", "o"]:
        result = get_info(elem, alpha_type=main_type)
        main_type = result[1]
        print(result)


Take 20 minutes and read over PEP8. It's Python's style guide and dictates how code should look. Most notably, you should have spaces after commas, and you shouldn't try to shove things onto one line in most cases (like you're doing with raise).



Your code is abusing try and raise. It doesn't make any sense to raise in a try just to go to the except. You should just use make use of the if that you already have:

def get_info(alpha, alpha_type):

    print("checking value of item:", alpha_type)

    if not alpha_type:
        return get_info(alpha, "vowel")

    else:
        return alpha, alpha_type




Honestly, I don't understand what you're trying to do here, so that's really all I can comment on. I just wanted to point out the formatting and needless use of try.