Oct-04-2019, 10:02 PM
1 2 3 4 5 6 |
def myfunc(n): return lambda a : a * n mydoubler = myfunc( 2 ) print (mydoubler( 11 )) |
what "return" means?
|
||||||||||||||||||||||||
Oct-04-2019, 10:02 PM
Oct-04-2019, 11:05 PM
return passes a value back to a calling function or method
Example:
Oct-09-2019, 12:50 PM
All functions return a value when called.
If a return statement is followed by an expression list, that expression list is evaluated and the value is returned: >>> def greater_than_1(n): ... return n > 1 ... >>> print(greater_than_1(1)) False >>> print(greater_than_1(2)) True
Oct-09-2019, 01:10 PM
Oct-09-2019, 01:37 PM
If interested in 'return' statement one can refer to Python built-in help:
Quote:The "return" statement
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Oct-09-2019, 01:40 PM
(Oct-09-2019, 01:10 PM)JTHilliard Wrote: Except for void functions, which do not return anything. There are no real void functions in Python, every function returns a value. If the function ends without an explicit return statement, the None singleton is returned. It may be None, but it is still a value.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures
Oct-09-2019, 04:22 PM
The really cool thing about the example OP gives is that the method returns a function. This points out that you can return any object, even complex objects like functions.
Oct-10-2019, 04:35 AM
(This post was last modified: Oct-10-2019, 04:35 AM by knackwurstbagel.)
You are returning a function, a lambda is an anonymous function. Functions in Python are objects and can be passed around, mydoubler is now a function and can be called. What is hapening is that each time you call myfunc with a different integer you can get back a unique function that returns that value * its argument. You could say this is like a factory for creating functions on the fly that you don't have to type out manually. This is the best explanation I can think of that would hopefully help. I would lookup How to use Python Lambda Functions There are a lot of resources that can explain both lambdas and functions that return functions. That's where I would start looking.
Oct-10-2019, 07:05 AM
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:
operator module are this kind of closures available.
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:
None .
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Oct-10-2019, 07:18 AM
(This post was last modified: Oct-10-2019, 07:18 AM by newbieAuggie2019.)
(Oct-10-2019, 07:05 AM)DeaD_EyE Wrote: ![]() https://python-forum.io/Thread-spam-eggs...or-newbies All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains." Steve Jobs | ||||||||||||||||||||||||
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Understand what it means that everything in Python is an object... | bytecrunch | 8 | 5,240 |
Mar-19-2021, 04:47 PM Last Post: nilamo |
|
effective means to flip boolean values? | Exsul | 3 | 5,171 |
Aug-25-2019, 03:58 PM Last Post: Exsul |
|
what means in Python the "->"? | lsepolis123 | 2 | 2,588 |
Aug-22-2019, 08:08 AM Last Post: DeaD_EyE |
|
Smtplib: What does context argument means? | Pythenx | 1 | 3,866 |
Mar-27-2019, 06:25 PM Last Post: nilamo |
|
what from .something means | sylas | 1 | 3,080 |
May-17-2018, 06:19 AM Last Post: buran |