Skip to content

Are closures not supported? #2583

Answered by Kodiologist
mmontone asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, closures are supported. Any Python code using closures will work the same if translated into Hy.

Your first example doesn't work because there's no built-in macro named setf. Note that the error message is actually NameError: name 'setf' is not defined, not UnboundLocalError.

Your second example doesn't work because your function tries to set x, which means it will create a new local variable for x, which is unset by the time of (+ x 1). Use (nonlocal x) to avoid this. In general, I'd recommend mastering Python scoping before trying to use let, which complicates things.

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by mmontone
Comment options

You must be logged in to vote
1 reply
@mmontone
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants