codeberg.org/fakefred/wikt-cli
loc nearly tripled, so has cursedness

nevertheless, having fun implementing pipes with

also just so that you're aware, if you do

def out_func():
funcs = []
for x in [0, 1]:
var = x
def in_func():
return var
funcs.append(in_func)
return funcs

print([f() for f in out_func()])

this looks like it's gonna print [0, 1] but it's not. It's gonna give you [1, 1].

@trickster @fakefred Just checking I understand: this might work properly if out_func was a generator rather than returning a list right?

Follow

@padraic_padraic @fakefred this happens because of the operational semantics in Python

def out_func():
funcs = []
for x in [0, 1]:
def in_func():
return x
funcs.append(in_func)
return funcs

`in_func` is a closure that closes over the `x` reference

every iteration in the `for` modifies `x` in place, it does not create a new `x` binding, so, of course, because all created closures point to the same x, the last version of x is the one that is returned, always

· · Web · 1 · 0 · 2

@padraic_padraic @fakefred this applies to any variable created or mutated inside of the `for`, because all vars are reused at every iteration

@padraic_padraic @fakefred but wait, there's more!

default arguments for functions are captured on the spot, so this piece of code can actually be made to work like this:

def out_func():
funcs = []
for x in [0, 1]:
def in_func(x=x):
return x
funcs.append(in_func)
return funcs

print([f() for f in out_func()])
# => [0, 1]

@trickster @fakefred Ah yeah, that I've heard of, because of this wild behaviour: nikos7am.com/posts/mutable-def

Which soem people abuse to add memoisation :ULTRACATE:

@trickster @fakefred I mean yeah, it's even part of functools these days if you absolutely must, functools.lru_cache.

I'm once again feeling the urge to learn rust

@padraic_padraic @fakefred this will work because (x=x) forces x to be "followed", we are technically not closing over it anymore, so x being a value type in this case, it's simply copied over

if x was a reference type, you would still be pointing to the same object in all closures

Sign in to participate in the conversation
Mastodon for Tech Folks

This Mastodon instance is for people interested in technology. Discussions aren't limited to technology, because tech folks shouldn't be limited to technology either!