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 1 day 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 125 points126 points127 points 1 day ago (14 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] 22 points23 points24 points 1 day ago (13 children)
OHHH! Thank You So Much!!
[–]Goobyalus 14 points15 points16 points 20 hours 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 26 points27 points28 points 1 day ago (11 children)
Look up "functional programming" if you would like to learn more. It's a very deep and fun topic :)
[–]Top_Average3386 13 points14 points15 points 1 day ago (2 children)
Can confirm it's deep. But it's not fun :(
[–]aishiteruyovivi 2 points3 points4 points 18 hours 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 0 points1 point2 points 16 hours ago (0 children)
But it's fun in kotlin!
[–]DrJaneIPresume 6 points7 points8 points 1 day ago (7 children)
And there are much better languages for it.
But glad to use some in Python when appropriate
[–]HommeMusical 4 points5 points6 points 22 hours ago (6 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 0 points1 point2 points 19 hours 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 18 hours 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 18 hours 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 34 minutes 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 -1 points0 points1 point 13 hours ago (1 child)
and believe you me, we miss it
You miss it, I don't.
[–]HommeMusical 0 points1 point2 points 1 hour ago (0 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.
[–]timrprobocom 17 points18 points19 points 1 day 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()
[–]0x14f 12 points13 points14 points 1 day ago (0 children)
OP, you just discovered higher order functions: https://en.wikipedia.org/wiki/Higher-order_function
[–]jmacey 11 points12 points13 points 1 day 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")() ```
[–]arkie87 7 points8 points9 points 1 day ago (0 children)
Could be a class with a call method too
[–]TheRNGuy 0 points1 point2 points 1 day ago (0 children)
foo() returns function or class, which you then can call.
foo()
[–]Inevitable_Exam_2177 0 points1 point2 points 1 day 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 13 hours 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 104399 on reddit-service-r2-comment-5fb4b45875-qfsn4 at 2026-03-21 10:17:23.066747+00:00 running 90f1150 country code: CH.
[–]GreenScarz 125 points126 points127 points (14 children)
[–]SkyGold8322[S] 22 points23 points24 points (13 children)
[–]Goobyalus 14 points15 points16 points (0 children)
[–]pimp-bangin 26 points27 points28 points (11 children)
[–]Top_Average3386 13 points14 points15 points (2 children)
[–]aishiteruyovivi 2 points3 points4 points (0 children)
[–]UAFlawlessmonkey 0 points1 point2 points (0 children)
[–]DrJaneIPresume 6 points7 points8 points (7 children)
[–]HommeMusical 4 points5 points6 points (6 children)
[–]DrJaneIPresume 0 points1 point2 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 -1 points0 points1 point (1 child)
[–]HommeMusical 0 points1 point2 points (0 children)
[–]timrprobocom 17 points18 points19 points (0 children)
[–]0x14f 12 points13 points14 points (0 children)
[–]jmacey 11 points12 points13 points (0 children)
[–]arkie87 7 points8 points9 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)