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

Howto - Python yield statement #1081

Open
RickDW opened this issue Mar 29, 2024 · 1 comment
Open

Howto - Python yield statement #1081

RickDW opened this issue Mar 29, 2024 · 1 comment

Comments

@RickDW
Copy link

RickDW commented Mar 29, 2024

Hi folks, not sure if this is the best place to get some advice, feel free to let me know if it's not.
I'm trying to implement a Python class which has one method that uses two yield statements. My current PyCall implementation is this, but it gives me a "SyntaxError: 'yield' outside function":

using PyCall

textual = pyimport("textual")
pyimport("textual.app")
pyimport("textual.widgets")


@pydef mutable struct StopwatchApp <: textual.app.App
    function compose(self)
        py"""
        yield textual.widgets.Header()
        yield textual.widgets.Footer()
        """
    end
end

The full error is this:

Traceback (most recent call last)
in <lambda>:1

/home/name/.julia/packages/PyCall/1gn3u/src/pyeval.jl:1
const Py_single_input = 256 # from Python.h

SyntaxError: 'yield' outside function

yield doesn't seem to be discussed in PyCall's docs so I'm wondering if there's an implementation for this. Please give me a shout if you can help me out :)

@stevengj
Copy link
Member

The Python yield statement is only valid inside a Python function — that is, your py"..." block needs to include a whole function def foo(...): ... and not just a function body.

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

No branches or pull requests

2 participants