Lame: Submit a PEP, campaign for community support, write a patch, go back and forth with the maintainers, endure weeks and months of bikeshedding, then maybe, eventually have your feature included in the next Python release.
Game: Use the codec hack, immediately publish your feature for all Python versions, then write "please do not use" to be safe.
This one is arguably even more of a hack; it's working at the source code level rather than the AST level.
The "coding" here is a bytes-to-text encoding. The Python lexer expects to see character data; you get to insert arbitrary code to convert the bytes to characters (or just use existing schemes the implement standards like UTF-8).
I think there's a package to treat Jupyter notebooks as source code (so you can import them as modules).
While the OP package is obviously a joke, the one with notebooks is kind of useful. And, of course, obligatory quote about how languages that don't have meta-programming at the design level will reinvent it, but poorly.
After using JS, Python dicts and objects feel so cumbersome. I don't see why they need to be separate things, and why you can't access a dict like `dict.key`. Destructuring is the icing on the cake. In JS, it even handles the named args use case like
const foo = ({name, age, email}) => { }
I'm guessing all of this has been proposed in Python before, and rejected in part because at this point it'd create way more confusion than it's worth.
I don’t mind the distinction of it as a map container keeping dot properties/methods separate from the keyed values. But yeah the endless string quoting is painful coming back from JS, bare key literals in constructors like JS would be a welcome addition for sure, as would named key unpacking like this whole post is about.
Yeah I had totally forgotten about this. I remember seeing it around a bit in the python 2 days when UTF-8 wasn’t always assumed. The fact a ~macro system can be bolted on using this is impressive, hilarious, and shockingly terrible.
Coming from lisp/haskell I always wanted destructuring but after using it quite a lot in ES6/Typescript, I found it's not always as ergonomic and readable as I thought.
They'll both trigger a runtime error, since the key you're using in the pattern (LHS) does not match any key in the dict.
Note that `'_'` is an actual string, and thus key, it's not any sort of wildcard. Using a bare `_` as key yields a syntax error, I assume because it's too ambiguous for the author to want to support it.
You shouldn't be using dicts for data that you know the name of anyway - use dataclasses or named tuples. Dicts are best for things with keys that are not known at compile time.
Now come on... for code golf? Why on Earth would anyone want extra syntax in a language with already tons of bloat in the syntax that contribute nothing to language's capabilities? It's, in Bill Gates words, like paying to make airplanes heavier...
This package is a funny gimmick, to illustrate, probably, unintended consequences of some of the aspects of Python's parser. Using this for anything other than another joke is harmful...
You have to pull them out by key name, and not just get everything. Here's a working version, though with a totally different syntax (to avoid having to list the keys twice, once as keys and once as resulting variable names):
>>> def u(locals, dct, keys):
... for k in keys:
... locals[k] = dct[k]
...
>>> dct = {'greeting': 'hello', 'thing': 'world', 'farewell': 'bye'}
>>> u(locals(), dct, ['greeting', 'thing'])
>>> greeting
'hello'
>>> thing
'world'
>>> farewell
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'farewell' is not defined
Modifying locals() is generally frowned upon, as there's no guarantee it'll work. But it does for this example.
[+] [-] xg15|8 months ago|reply
Game: Use the codec hack, immediately publish your feature for all Python versions, then write "please do not use" to be safe.
[+] [-] kristjansson|8 months ago|reply
https://peps.python.org/pep-0636/#matching-builtin-classes
[+] [-] xg15|8 months ago|reply
Will this allow combinations of bound and unbound variables?
E.g.:
Seems both useful and potentially confusing.[+] [-] zdimension|8 months ago|reply
[+] [-] zahlman|8 months ago|reply
The "coding" here is a bytes-to-text encoding. The Python lexer expects to see character data; you get to insert arbitrary code to convert the bytes to characters (or just use existing schemes the implement standards like UTF-8).
[+] [-] crabbone|8 months ago|reply
While the OP package is obviously a joke, the one with notebooks is kind of useful. And, of course, obligatory quote about how languages that don't have meta-programming at the design level will reinvent it, but poorly.
[+] [-] unknown|8 months ago|reply
[deleted]
[+] [-] notpushkin|8 months ago|reply
[1]: https://pypi.org/p/future-fstrings, mentioned in https://github.com/asottile/dict-unpacking-at-home#please-do...
[+] [-] zelphirkalt|8 months ago|reply
[+] [-] peter422|8 months ago|reply
[+] [-] yde_java|8 months ago|reply
It gives dict unpacking but also a shorthand dict creation like this:
[0] https://github.com/alexmojaki/sorcery[+] [-] john-radio|8 months ago|reply
[+] [-] frollogaston|8 months ago|reply
[+] [-] tkcranny|8 months ago|reply
[+] [-] nine_k|8 months ago|reply
[+] [-] tkcranny|8 months ago|reply
[+] [-] agumonkey|8 months ago|reply
[+] [-] sco1|8 months ago|reply
[+] [-] agumonkey|8 months ago|reply
[+] [-] qwertox|8 months ago|reply
[+] [-] masklinn|8 months ago|reply
They'll both trigger a runtime error, since the key you're using in the pattern (LHS) does not match any key in the dict.
Note that `'_'` is an actual string, and thus key, it's not any sort of wildcard. Using a bare `_` as key yields a syntax error, I assume because it's too ambiguous for the author to want to support it.
[+] [-] qexat|8 months ago|reply
[+] [-] sametmax|8 months ago|reply
[+] [-] mixmastamyk|8 months ago|reply
As if dropping that word is some sort of justification. I don’t know what the opinion is! Worse is better?
[+] [-] nikisweeting|8 months ago|reply
[+] [-] IshKebab|8 months ago|reply
[+] [-] almostgotcaught|8 months ago|reply
[+] [-] crabbone|8 months ago|reply
This package is a funny gimmick, to illustrate, probably, unintended consequences of some of the aspects of Python's parser. Using this for anything other than another joke is harmful...
[+] [-] ziofill|8 months ago|reply
{greeting, thing} = dct
is a set, which is not ordered, so why would greeting and thing be assigned in the order in which they appear?
[+] [-] xg15|8 months ago|reply
[+] [-] andy99|8 months ago|reply
*I realize the tuple can be omitted here
[+] [-] Izkata|8 months ago|reply
[+] [-] sischoel|8 months ago|reply
[+] [-] Grikbdl|8 months ago|reply
[+] [-] masklinn|8 months ago|reply
[+] [-] unknown|8 months ago|reply
[deleted]
[+] [-] dogukancey|8 months ago|reply
[deleted]
[+] [-] unit149|8 months ago|reply
[deleted]
[+] [-] odyssey7|8 months ago|reply
[+] [-] nikisweeting|8 months ago|reply
- https://pypi.org/project/python-benedict/ - https://docs.pydantic.dev/ - https://github.com/alexmojaki/sorcery