Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample code for the article on closures #581

Merged
merged 7 commits into from
Oct 3, 2024
Merged

Conversation

lpozo
Copy link
Contributor

@lpozo lpozo commented Sep 13, 2024

Where to put new files:

  • New files should go into a top-level subfolder, named after the article slug. For example: my-awesome-article

How to merge your changes:

  1. Make sure the CI code style tests all pass (+ run the automatic code formatter if necessary).
  2. Find an RP Team member on Slack and ask them to review & approve your PR.
  3. Once the PR has one positive ("approved") review, GitHub lets you merge the PR.
  4. 🎉


def outer_func():
name = "Pythonista"
return lambda name=name: print(f"Hello, {name}!")
Copy link
Contributor

@bzaczynski bzaczynski Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Fact Check

@lpozo Actually, I just realized this is the wrong syntax for a lambda expression. This is the correct one:

Suggested change
return lambda name=name: print(f"Hello, {name}!")
return lambda name: print(f"Hello, {name}!")

Unfortunately, this mistake is also present in the tutorial text.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work if we don't set the argument:

>>> def outer_func():
...     name = "Pythonista"
...     return lambda name: print(f"Hello, {name}!")
...     
... 
>>> func = outer_func()
>>> func()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    func()
TypeError: outer_func.<locals>.<lambda>() missing 1 required positional argument: 'func'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct syntax is:

>>> def outer_func():
...     name = "Pythonista"
...     return lambda: print(f"Hello, {name}!")
...     
... 
>>> func = outer_func()
>>> func()
Hello, Pythonista!

Copy link
Contributor

@bzaczynski bzaczynski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lpozo There's one small detail to fix in the lambda expression example (same in the tutorial text.)

@brendaweles brendaweles merged commit 8e06d4e into master Oct 3, 2024
1 check passed
@brendaweles brendaweles deleted the python-closure branch October 3, 2024 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants