Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
return in a function
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Python dictionary.
dict = {'alex' : 1,
        'larry' : 2, 
        'mark' : 3}

print(dict['mark'])
Reply
#3
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.
Serafim, buran, ndc85430 like this post
Reply
#4
Nice. I didn't realise 3.10 had pattern matching.
Reply
#5
(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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Function A could just call
def A():
    ...
    return B()
Reply
#7
what if the "bunch of code" only returns in some cases and "falls through" in others?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
You could do like list.index()
def A():
    try:
        return B(value)
    except ValueError:
        pass
Reply
#9
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
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
Gribouillis likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is that possible to have a new feature: dynamic return with using function argument kouui 2 1,036 Aug-26-2019, 10:05 AM
Last Post: kouui

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020