Python Forum
return in a function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: return in a function (/thread-32790.html)

Pages: 1 2


return in a function - Skaperen - Mar-06-2021

suppose i have a bunch of code like
if first_name == 'alex':
    return 1
if middle_name == 'larry':
    return 2
if middle_name == 'mark':
    return 3
if first_name == 'sarah':
    return 4
if surname == 'smith':
    return 5
duplicated in several places and i want to reduce the duplication by putting it in a function. how would i do that?


RE: return in a function - michael1789 - Mar-06-2021

Python dictionary.
dict = {'alex' : 1,
        'larry' : 2, 
        'mark' : 3}

print(dict['mark'])



RE: return in a function - snippsat - Mar-06-2021

Dictionary as mention bye michael1789,but don't use dict as Python use that name.
Can throw in in get to handle missing key,and give a familiar name of what is called other languages.
def switch_case(record, search):
    return record.get(search, f'<{search}> not in record')

if __name__ == '__main__':
    record = {
        'alex': 1,
        'larry': 2,
        'mark': 3
        }
    search = 'mark'
    print(switch_case(record, search))
Need or not,so to we get switch case version in Python 3.10.
Called Structural Pattern Matching which is more powerful that just simple switch case.


RE: return in a function - ndc85430 - Mar-06-2021

Nice. I didn't realise 3.10 had pattern matching.


RE: return in a function - Skaperen - Mar-06-2021

(Mar-06-2021, 05:13 AM)michael1789 Wrote: Python dictionary.
dict = {'alex' : 1,
        'larry' : 2, 
        'mark' : 3}

print(dict['mark'])

the example i gave involves testing various different variables. maybe it's a bad example. the "bunch of code" can't be changed. the question is about how to have function A call function B and have function B effect the return all the way to the caller of function A. put the "bunch of code" inside function B so function A can just call function B in all those places it had that "bunch of code". but what about those returns? can function B do a return for function A? not really. see the complication?


RE: return in a function - Gribouillis - Mar-07-2021

Function A could just call
def A():
    ...
    return B()



RE: return in a function - Skaperen - Mar-07-2021

what if the "bunch of code" only returns in some cases and "falls through" in others?


RE: return in a function - Gribouillis - Mar-08-2021

You could do like list.index()
def A():
    try:
        return B(value)
    except ValueError:
        pass



RE: return in a function - Skaperen - Mar-08-2021

so do i get to put the duplicated and complicated decision making code in a function? i'm guessing B() should raise an exception. then A() gets the exception or A()'s caller does.


RE: return in a function - nilamo - Mar-11-2021

I would use a sentinel value (None, if that's not valid) to mark a Not Found case. So for this:
def lookup(first_name, middle_name, surname):
    if first_name == 'alex':
        return 1
    if middle_name == 'larry':
        return 2
    if middle_name == 'mark':
        return 3
    if first_name == 'sarah':
        return 4
    if surname == 'smith':
        return 5
    return None

if (res := lookup(f, m, s)) is None:
    # not found