Welcome to MadPy!


The MadPy Magpie

Organizers¶

Ed Rogers David Hoese Josh Karpel

Ed Rogers

David Hoese

Josh Karpel

Code of Conduct¶

MadPy is a community group and open to all experience levels. We are committed to a safe, professional environment

Our Commitment to You¶

We are enthusiastically focused on improving our event and making it a place that is welcoming to all. All reports will be taken seriously, handled respectfully, and dealt with in a timely manner.

Learn more about the MadPy Code of Conduct:

https://github.com/madison-python/code-of-conduct

Python Warm-Up¶

Calling Yield Multiple Times¶

In [1]:
def some_things():
    yield 1
    yield 2
    yield 3
In [2]:
def them():
    for thing in some_things():
        yield thing

What happens when we call list(them())?

In [3]:
list(them())
Out[3]:
[1, 2, 3]

Changing them() to return a list instead of a generator¶

In [4]:
def some_things_as_a_list():
    return list(some_things())
In [5]:
def them():
    return some_things_as_a_list()  # Using return
                                    # instead of the yield call
    for thing in some_things():
        yield thing

What will list(them()) return?

In [6]:
list(them())
Out[6]:
[]

Taking a closer look at them()¶

In [7]:
def them():
    return some_things_as_a_list()
    # for thing in some_things():
    #     yield thing
In [8]:
list(them())
Out[8]:
[1, 2, 3]

Because yield appears in the function body, Python compiles them as a generator function — at compile time, before any code runs.

Calling them() returns a generator object without executing anything. The return value is silently ignored during iteration.

Want more MadPy?¶

MadPymadpy.com
Meetupmeetup.com/madpython
GitHubgithub.com/madison-python
Mastodonfosstodon.org/@madpy
Slackslack.madpy.com
LinkedInlinkedin.com/company/madpy

The Best Way to Help MadPy¶

Talk to your employer about Sponsorship!¶

Logo for the MadPy talk