1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ You could abuse a mutable default argument to persist state. But that would be a very ugly hack. If you need to keep a state, either do it separately in a variable external to the function (if it is a state of the surrounding code), or make this into a class which can keep the state together with the method (if it is a state inherently tied to the function). \$\endgroup\$ – Graipher Nov 11 '19 at 8:43
  • \$\begingroup\$ With abuse I mean something like this. \$\endgroup\$ – Graipher Nov 11 '19 at 8:51
  • \$\begingroup\$ I don't get it, if instead of your for elem in ["a", "e", "o"] I use for elem in "abc" I also have 3 vowels... What's the point? \$\endgroup\$ – 301_Moved_Permanently Nov 11 '19 at 11:11
  • \$\begingroup\$ Consider the list of alphabets as list of links and the alpha_type is a random proxy. The above function is supposed to reuse the samy proxy until invalid. Thanks. \$\endgroup\$ – robots.txt Nov 11 '19 at 12:10
  • \$\begingroup\$ Considering the output isn't as expected, the code doesn't work properly. Please take a look at the help center. \$\endgroup\$ – Mast Nov 11 '19 at 12:30
3
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.