You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromitertoolsimportrepeatfromunpythonic.syntaximportmacros, autotrunc# DOES NOT EXIST YETwithautotrunc:
a, b, c=repeat(None)
Concise, and we don't have to manually repeat the unimportant detail of how many names are on the LHS, since this information is already lexically available (just count from the AST).
The equivalent pure Python is:
fromitertoolsimportrepeata, b, c=repeat(None, 3)
The solution must be a block macro, because it must have the assignment LHS in its context in order to count the names there. We can't do anything about this in the function call, or even as an expr macro for the RHS - at that point it's not yet known where the output is going to be sent.
However, is this so useful after all? We need to be careful with edge cases such as:
a, *b, c=repeat(42) # error, an infinite iterable has no last item*a, b, c=repeat(42) # error, likewisea, b, *c=repeat(42) # ok, a = 42, b = 42, c = repeat(42)
In the first example, for the specific case of repeat we could define it to extract one 42 and assign it to c, but that doesn't generalize. It's better to refuse the temptation to guess.
Unpacking the start of an arbitrary infinite iterable can be trivially defined by delegating to unpythonic.it.unpack. The third example could expand to:
This would be occasionally useful:
Concise, and we don't have to manually repeat the unimportant detail of how many names are on the LHS, since this information is already lexically available (just count from the AST).
The equivalent pure Python is:
The solution must be a block macro, because it must have the assignment LHS in its context in order to count the names there. We can't do anything about this in the function call, or even as an expr macro for the RHS - at that point it's not yet known where the output is going to be sent.
However, is this so useful after all? We need to be careful with edge cases such as:
In the first example, for the specific case of
repeat
we could define it to extract one42
and assign it toc
, but that doesn't generalize. It's better to refuse the temptation to guess.Unpacking the start of an arbitrary infinite iterable can be trivially defined by delegating to
unpythonic.it.unpack
. The third example could expand to:(Since an assignment always appears in a block of statements, we can always prepend the import.)
Another case to consider are finite inputs. For example, if
len(seq) == 5
:Python itself already knows to do the right thing here. How would
autotrunc
avoid putting its nose where it doesn't belong?The text was updated successfully, but these errors were encountered: