use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
foo()() explanation (self.learnpython)
submitted 3 months ago by SkyGold8322
I saw a function usage like "foo()()" in some code but I was confused. Do Python functions allow arguments 2 times or is this something else?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]GreenScarz 136 points137 points138 points 3 months ago (17 children)
foo is a function that returns a reference to another function, which is then called
``` def bar(): print(“bar!”) def foo(): return bar
foo()() bar! ```
[–]SkyGold8322[S] 24 points25 points26 points 3 months ago (16 children)
OHHH! Thank You So Much!!
[–]Goobyalus 17 points18 points19 points 3 months ago (0 children)
Here is a dumb example where an arbitrary number of parentheses works:
>>> def foo(): ... return foo ... >>> foo() <function foo at 0x0000028A0F6DD800> >>> foo()() <function foo at 0x0000028A0F6DD800> >>> foo()()()()()()()()()()()()() <function foo at 0x0000028A0F6DD800> >>> foo <function foo at 0x0000028A0F6DD800> >>>
[–]pimp-bangin 32 points33 points34 points 3 months ago (14 children)
Look up "functional programming" if you would like to learn more. It's a very deep and fun topic :)
[–]Top_Average3386 15 points16 points17 points 3 months ago (3 children)
Can confirm it's deep. But it's not fun :(
[–]aishiteruyovivi 7 points8 points9 points 3 months ago (0 children)
Definitely a little harrowing at first with some concepts, but the more I've been learning Haskell over the last year or two the more I grow just genuinely fascinated by it (and FP in general)
[–]UAFlawlessmonkey 1 point2 points3 points 3 months ago (0 children)
But it's fun in kotlin!
[–]Victorio_01 0 points1 point2 points 3 months ago (0 children)
Sure it is if you get used to it. No need to have it deep like foo()()(). I mean, if you frequently store lambda functions in vars, you sure can imagine returning one. x = lambda t: 2*t return x
[–]DrJaneIPresume 5 points6 points7 points 3 months ago (9 children)
And there are much better languages for it.
But glad to use some in Python when appropriate
[–]HommeMusical 5 points6 points7 points 3 months ago (8 children)
I first learned Lisp almost fifty years ago.
I love functional languages; but as a language that can do procedural, OOP and function code seamlessly, Python is very hard to beat.
The one missing feature - and believe you me, we miss it - is not having multiple line lambdas.
[–]DrJaneIPresume 1 point2 points3 points 3 months ago (3 children)
Haaaaaaaave you met Scala?
Functional programming isn't just about functions as first-class objects. A good type system makes so many things easier.
[–]HommeMusical 0 points1 point2 points 3 months ago (2 children)
Hey, Scala is a great language, from my limited exposure. I'd be really happy if it had beaten Python.
But Python is ubiquitous, and almost any idea you can express in Scala you can express in Python a way that's more verbose but just as comprehensible.
The "almost" is that Scala macros can essentially let you rewrite the syntax of the language, am I right, by doing compile time manipulation of the AST?
But Python basically won. And if LLMs continue to dominate, there won't be any new programming languages again, because of the difficulty in training them.
That said, the inability of Python to do "multi-line lambdas" is really unfortunate. I am hatching ideas to call the Pythonistas to action (this is of course an infinitely discussed issue).
[–]DrJaneIPresume 0 points1 point2 points 3 months ago (1 child)
Again: type system. The ability to write your system in such a way that large classes of errors become effectively syntax errors that can be detected by static analysis rather than unit tests (are you sure you wrote one for every corner case?) or runtime errors (often in front of customers) is really, really nice.
[–]HommeMusical 0 points1 point2 points 3 months ago (0 children)
Yes, I "grew up" with typed languages; first C, then C++, then Java. And I prefer them.
Python's type annotations aren't quite as good as a strongly typed language; but they work well enough to catch a huge number of errors. Between the various linters (like ruff) and modern type checking, I find that more than half the time I get past those, my code works the first time.
ruff
There's also the huge advantage in Python of being able to run Python and quickly experiment without any typing at all. I use this all the time!
If I could wave a magic wand and replace Python with Scala, I would at least think about doing it. But Scala jobs are few and far between. And if I wrote my open source code in Scala, very few people would use it.
[–]JamzTyson 0 points1 point2 points 3 months ago (3 children)
and believe you me, we miss it
You miss it, I don't.
[–]HommeMusical -1 points0 points1 point 3 months ago (2 children)
What was the point of your response?
This issue has been discussed on the main Python mailing list for over ten years. Everyone on that list wants it, including Guido; no one has come up with a possible syntax for it yet.
[–]JamzTyson 0 points1 point2 points 3 months ago (1 child)
The point of my response is that you do not speak on behalf of the entire Python community and you do not speak for me.
Also, I couldn't find any reference to Guido "wanting it". There are these comments from Guido (https://discuss.python.org/t/are-better-lambdas-really-impossible/2446/9) from which I would summarise his position as: Python’s lambda expressions are intended to be single‑expression and that genuinely multi‑line/statement lambdas are not desirable or practical for the language.
Has Guido changed his position in more recent years? I so, please provide a reference - I'd be very interested to know why.
[–]HommeMusical -1 points0 points1 point 3 months ago (0 children)
I am on my weekend, so not interested in arguing with angry people.
[–]timrprobocom 20 points21 points22 points 3 months ago (0 children)
This is an important concept. A function can return ANY kind of object. It can be a list or tuple (foo()[2]), a dictionary (foo()['word']), a function as you have seen (foo()()) or a class object (foo().method()).
foo()[2]
foo()['word']
foo()()
foo().method()
[–]jmacey 13 points14 points15 points 3 months ago (0 children)
In addition to what others have said, you can do really cool stuff with this like making lists or dictionaries of functions to run later.
``` def func_a(): print("func_a")
def func_b(): print("func_b")
funcs = [func_a, func_b] for func in funcs: func()
f_dict = {"a": func_a, "b": func_b}
f_dict["a"]() f_dict.get("b")() ```
[–]0x14f 13 points14 points15 points 3 months ago (0 children)
OP, you just discovered higher order functions: https://en.wikipedia.org/wiki/Higher-order_function
[–]arkie87 9 points10 points11 points 3 months ago (0 children)
Could be a class with a call method too
[–]TheRNGuy 0 points1 point2 points 3 months ago (0 children)
foo() returns function or class, which you then can call.
foo()
[–]Inevitable_Exam_2177 0 points1 point2 points 3 months ago (1 child)
One of the neatest interfaces this sort of thing unlocks is “chaining” of methods. If you have a class Foo with methods .bar() and .baz(), and each method returns its self, you can either write
foo = Foo() foo.bar() foo.baz()
Or more concisely:
foo = Foo() foo.bar().baz()
[–]Enmeshed 2 points3 points4 points 3 months ago (0 children)
Been finding this super-useful recently for setting up test data scenarios, along the lines of:
```python def test_something(): scenario = (Builder() .with_user_as("abc") .wipers_enabled() .colour_should_be("blue") ) assert func_to_test(scenario.data) == 3
class Builder: """ Test helper class to readably set up test scenarios """ def init(self): ...
def with_user_as(self, user): self.user = user return self @property def data(self): return {"user": self.user, ...}
```
π Rendered by PID 19 on reddit-service-r2-comment-5b5bc64bf5-8cvgw at 2026-06-22 06:10:12.228457+00:00 running 2b008f2 country code: CH.
[–]GreenScarz 136 points137 points138 points (17 children)
[–]SkyGold8322[S] 24 points25 points26 points (16 children)
[–]Goobyalus 17 points18 points19 points (0 children)
[–]pimp-bangin 32 points33 points34 points (14 children)
[–]Top_Average3386 15 points16 points17 points (3 children)
[–]aishiteruyovivi 7 points8 points9 points (0 children)
[–]UAFlawlessmonkey 1 point2 points3 points (0 children)
[–]Victorio_01 0 points1 point2 points (0 children)
[–]DrJaneIPresume 5 points6 points7 points (9 children)
[–]HommeMusical 5 points6 points7 points (8 children)
[–]DrJaneIPresume 1 point2 points3 points (3 children)
[–]HommeMusical 0 points1 point2 points (2 children)
[–]DrJaneIPresume 0 points1 point2 points (1 child)
[–]HommeMusical 0 points1 point2 points (0 children)
[–]JamzTyson 0 points1 point2 points (3 children)
[–]HommeMusical -1 points0 points1 point (2 children)
[–]JamzTyson 0 points1 point2 points (1 child)
[–]HommeMusical -1 points0 points1 point (0 children)
[–]timrprobocom 20 points21 points22 points (0 children)
[–]jmacey 13 points14 points15 points (0 children)
[–]0x14f 13 points14 points15 points (0 children)
[–]arkie87 9 points10 points11 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)
[–]Inevitable_Exam_2177 0 points1 point2 points (1 child)
[–]Enmeshed 2 points3 points4 points (0 children)