Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what "return" means?
#9
def myfunc(n):
    return lambda a : a * n
is similar to this:
def myfunc(n):
    def inner(a):
        return a * n
    return inner
In both cases the function myfunc returns a function.
The lambda expression is an anonymous function. In both cases the function myfunc is a closure.
This means inside the function is another one, which has access to the stack of the parant function.

Calling this function looks strange:
n = 10
a = 20
result = myfunc(n)(a)
print(result)
You can bring this back a normal form:
def myfunc(n, a):
    return a * n
Calling it:
result = myfunc(10,20)
Closures are used for decorators and functional style. For example inside the operator module are this kind of closures available.

import operator

# list with dicts
mylist1 = [dict(value=10, name='c'), dict(value=20, name='b'), dict(value=30, name='a')]
# we want to sort by name, no problem:
by_name = sorted(mylist1, key=operator.itemgetter('name'))
by_value = sorted(mylist1, key=operator.itemgetter('value'))
The sorted function uses the key to sort. It must be a callable and for each item in the list, the callable is called with. The callable should return something, that can be used for sorting. For example it may return str, then it's sorted lexically or it returns numbers or tuples with numbers. Mostly you see in examples for sorting the lambda expression.

In tutorials about sorting, you see often this:
by_name = sorted(mylist1, key=lambda x: x['name'])
by_value = sorted(mylist1, key=lambda x: x['value'])
Another thing you have to know, that functions return implicit None.

def foo():
    print('Hello foo')

ret_val = foo()
print(f"Return value of foo is: {ret_val}\nIt's type is: {type(ret_val)}")
Output:
Hello foo Return value of foo is: None It's type is: <class 'NoneType'>
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
what "return" means? - by PY_beginner - Oct-04-2019, 10:02 PM
RE: what "return" means? - by Larz60+ - Oct-04-2019, 11:05 PM
RE: what "return" means? - by prih_yah - Oct-09-2019, 12:50 PM
RE: what "return" means? - by perfringo - Oct-09-2019, 01:37 PM
RE: what "return" means? - by ichabod801 - Oct-09-2019, 01:40 PM
RE: what "return" means? - by jefsummers - Oct-09-2019, 04:22 PM
RE: what "return" means? - by knackwurstbagel - Oct-10-2019, 04:35 AM
RE: what "return" means? - by DeaD_EyE - Oct-10-2019, 07:05 AM
RE: what "return" means? - by newbieAuggie2019 - Oct-10-2019, 07:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Understand what it means that everything in Python is an object... bytecrunch 8 3,961 Mar-19-2021, 04:47 PM
Last Post: nilamo
  effective means to flip boolean values? Exsul 3 4,432 Aug-25-2019, 03:58 PM
Last Post: Exsul
  what means in Python the "->"? lsepolis123 2 2,058 Aug-22-2019, 08:08 AM
Last Post: DeaD_EyE
  Smtplib: What does context argument means? Pythenx 1 3,160 Mar-27-2019, 06:25 PM
Last Post: nilamo
  what from .something means sylas 1 2,654 May-17-2018, 06:19 AM
Last Post: buran

Forum Jump:

User Panel Messages

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