Reputation: 137
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.
As I'm not supplying the value of type
from outer side of the function, I've defined it as None. However, 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.
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.
I've tried with:
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__':
for elem in ["a","e","o"]:
print(get_info(elem,alpha_type=None))
Output it 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')
Output I wish to have:
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')
How can I reuse the value of previous type instead of None?
Btw, I'm after any solution which keeps the existing design intact.
Upvotes: 0
Views: 51
Reputation: 3495
Currently you are passing from the main alpha_type=None
every time. If you want to pass the last returned value instead, change the main to:
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:
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')
Upvotes: 1