[Python-ideas] Re: Proposal: Keyword Unpacking Shortcut [was Re: Keyword arguments self-assignment]
David Mertz wrote: > [...] Many of the new syntax ideas COULD be done with an > arcane function that only needs to be written once (but better than my 15 > minute versions). The fact that such magic functions are not in widespread > use, to my mind, argues quite strongly against them actually meriting new > syntax. People can live without this syntax, the rationale of the proposal ins't that users are actively complaining about this and if people aren't expressing anything about this issue isn't real reason to rule out an idea. Type hints may be an example, I guess... We could keep using comments for that or other hacks, no new syntax needed. But in order for people who would benefit from it to start actually using it to make it as easy as possible was the push this feature needed for gaining wider adoption. Correct me if I'm off here please. The proposal intends to allow for better code and to serve as an incentive for the widespread use of keyword parameters. A utility function `Q` won't have high adherence because it is essentially a hack and no one wants to install a lib and go importing `Q` in every project file. If it's not easy and/or doesn't look nice people won't use it. This is only my humble opinion though. Rodrigo Martins de Oliveira ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/VRNDZ33ZMHDSWETIOBGIUGJGT4B2MCZL/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
Thanks @ChrisAngelico! I will get to it. Once a first draft is ready I'll share the github link in here. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/VS4ZIVYK7ZDAINCRG5OBTOOTEFHFKWTR/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
@AndrewBarnert yes I should have thought it better that there is no need to use `kwargs` forcibly. My bad. I am a little more convinced of this being a solid candidate solution to the problem. Thanks for talking me through! Rodrigo Martins de Oliveira ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/6DHRZBUHEZ7EW5UX2O55XOVHKNQC4FLN/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
@ChrisAngelico @EricVSmith Thank you for be willing to sponsor the PEP. I think it may be best to start writing it already, as I see it, the proposal wasn't a clear no (nor a clear yes but if it was to be, probably someone else would already have proposed something in these lines and we wouldn't be discussing this right now). I've thought of this proposal for 2 years from now and I still think to date that it would be a really nice feature. Like @ChrisAngelico, I have written and reviewed so many different code that would benefit from such a feature that I do believe this deserves consideration from the community. If this ends up on we rejecting the idea that's fine, at least we give closure to this. I do wish to carry on with writing a PEP with your help guys. What's the next step? ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/72E47UPAADUUGNWHHIJ3AEX5UYLJCM2L/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
@ChrisAngelico I do prefer the original proposal, though I do see the point of it being harder for beginner to understand. The mode-switch proposal though would not impede one to mix shorthand and longhand forms. This should be valid syntax: ```python return render_template("index.html", *, twitter, username=user["display_name"], channel, channelid, error, setups=database.list_setups(channelid), sched_tz, schedule, sched_tweet, checklist=database.get_checklist(channelid), timers=database.list_timers(channelid), tweets, ) ``` I'll wait the weekend is through to then assess if we can reach consensus or just reject the proposal. Rodrigo Martins de Oliveira ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/56VVQFTLNDALBDPY6B2RLCEYIZHMCG7N/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
I believe this is a different feature, non-exclusive to the one proposed here, that would also make it possible not to re-declare keywords. But implementing this change with the argument of making function calls less repetitive or verbose when having redundant named keywords and variables doesn't sell it to me. See, function calls would still suffer to be less redundant if we go with this: ```python def foo(a, b, **kwargs): c = ... bar(**{:a, :b, :c, d: kwargs["d"]}) # this just got worse ``` ```python def foo(a, b, **kwargs): c = ... # all parameters definition is away from the function call, not a fan # one can possibly overwrite some key on kwarg without knowing kwargs.update({:a, :b, :c}) bar(**kwargs) ``` ```python def foo(a, b, **kwargs): c = ... bar(**(kwargs | {:a, :b, :c})) # a little better but one can still overwrite some key on kwarg without knowing ``` Using a "magical" separator does the job and has little interactions with other syntaxes, using the `*` character seems better than just picking another random one (like we did with `/`). Comparing with all the above excerpts, this is still more appealing and clearer for me: ```python def foo(a, b, **kwargs): c = ... bar(*, a, b, c, **kwargs) # also, if any of `a`, `b` or `c` is in `kwargs` we get a proper error ``` Rodrigo Martins de Oliveira ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/ATCTNM5DTDTXLLCOFEHDHM7OP2MYTQDW/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
@StevenDAprano we are discussing alternative syntaxes to enhance readability and in place of blank assignments `k=` we could possibly use a sole `*` character as indicative that the following parameters are all to be passed as keywords. In this syntax, the keyword to which the parameter will be assigned can be explicit as in `f(keyword=something)`, i.e. `something` is assigned to the `keyword` argument; or implicitly as in `f(*, something)`, i.e. `something` is assigned to the argument with same name of the parameter. This thread is actually addressed to this reply: oliveira.rodrigo.m@gmail.com wrote: > @StevenDAprano and this goes for @RhodriJames , thank you for sharing your > point of > view. Indeed the proposed syntax is obscure and would not be that readable for > beginners. > Couldn't we work around this so? The concept is still good for me just the > syntax that > is obscure, maybe something like this would work: > # '*' character delimits that subsequent passed > parameters will be passed > # as keyword arguments with the same name of the variables used > self.do_something(positional, *, keyword) > # this would be equivalent to: > self.do_something(positional, keyword=keyword) > > I believe this is readable even if you don't know Python: positional and > keyword are being passed as parameters, the * character is > mysterious at first but so it is in def foo(a, *, b) and it doesn't get into > the way of basic readability. > Rodrigo Martins de Oliveira I believe some of us just clicked "Reply" on Mailman 3 and started nesting threads. Sorry. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/WPSIWC3D77XVZ3SJDS435XJNJ2N5QUBC/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
@Steven D'Aprano see that it doesn't actually have to look like a pair, it doesn't need to be one at all. Just like in: ```python def f(a): ... f(x) ``` `x` is implicitly assigned with `a`, i.e. this is `x = a` under the hood and there is no need to think of a key-value pair `x: a`. The same would go for the discussed assignment of keyword args using the `*` separator syntax (which seems more appealing to the ones in this thread than the original proposal): ```python def f(a, b): ... f(*, b, a) ``` The `*` would indicate that the following parameters are all keyword parameters, where the order doesn't matter and, unless explicitly defined, each passed parameter will be assigned to the argument with same name. So, under the hood you get `a = a` and `b = b`. In this syntax one can choose whether or not to explicitly defined the value of a keyword, so these statements would be equivalent: ```python f(positional, *, keyword0, keyword1=explicit, keyword2) f(positional, keyword0=keyword0, keyword1=explicit, keyword2=keyword2) ``` I'm against subverting dictionary literals declaration to support implicit pairs for the sake of readability. If that was the way we're going to implement this feature than I make your point mine, something like this just looks like a weird set not a dict: ```python kw = { , x, y, } f(**kw) ``` In Javascript ES6 they don't have sets built like python so `{}` always refers to objects being constructed. It does indeed support implicit key: value pairs, so in ES6 `{ a: a, b: x, c: c }` is equivalent to `{ a, b: x, c }`. This is okay for Javascript users because they would not thought it as sets and the only obvious assumption to make is that parameters are being implicitly assigned to members. This is not the case in Python so I would refrain from changing dictionary literals syntax. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/S4EXST7E3UOEXSOXHQKE6I5HTBFMEIOE/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
You have a fair point @DominikVilsmeier though I still believe this can workout there are other alternatives up for discussion such as using the `=` character. You see: after a keyword parameter you cannot define other positional ones, so the new syntax can assume all parameters following a first keyword parameter are to be captured if not explicitly declared. So we can have a syntax in which these three statements are the same: ```python foo(a, b=b, c=c) foo(a, b=b, c) foo(a, =, b, c) ``` But in this option I find `foo(a, b=b, c)` obscure that `c` will be captured as keyword argument. For now I still believe we can live with the `*` being slightly asymmetric. For the record, the positional-only specifier `/` already has no other real meaning other than delimiting different types of arguments. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/4LY2OLXQAO5B2LDCDVNCVRXA6ZW7VTVT/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
Thanks @AlexHall I believe your version is even better in readability. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/KLM4QNY62YZ2KOESK45YSCMAN7LB2YIQ/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
I'm really open to discuss how we can achieve this feature with clearer syntax than the first proposed version. If any of you have more ideas please share :) @EricVSmith I didn't thought it through about the syntax with the `*` character but your case is well covered: ``` self.do_something( positional, keyword1=somethingelse, *, keyword, keyword2, ) ``` Rodrigo Martins de Oliveira ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/SL3LRPX7EETL3DFNPZ6CE3YSWUSVIHQO/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
@ > Do any other languages already have this feature? JavaScript ES6 has similar feature: ```javascript x = 1 y = 2 do_something({ x, y }) ``` ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/64SUXRENNQDDE3YH6FLTKUR26QEMBK3R/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
@StevenDAprano and this goes for @RhodriJames , thank you for sharing your point of view. Indeed the proposed syntax is obscure and would not be that readable for beginners. Couldn't we work around this so? The concept is still good for me just the syntax that is obscure, maybe something like this would work: ```python # '*' character delimits that subsequent passed parameters will be passed # as keyword arguments with the same name of the variables used self.do_something(positional, *, keyword) # this would be equivalent to: self.do_something(positional, keyword=keyword) ``` I believe this is readable even if you don't know Python: `positional` and `keyword` are being passed as parameters, the `*` character is mysterious at first but so it is in `def foo(a, *, b)` and it doesn't get into the way of basic readability. Rodrigo Martins de Oliveira ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/N2ZY5NQ5T2OJRSUGZJOANEQOGEQIYYIK/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Keyword arguments self-assignment
Thanks for pointing the previous discussion @ChristopherBarker Proposals are similar but the scope here is limited to function calls which does prevent a bunch of issues already pointed out in the previous discussion. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/Z5FA5JGYM6GFFCYG3HJWWV67IMNOWB3A/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Keyword arguments self-assignment
I'm opening this thread to discuss and collect feedback on a language change to support keyword arguments to be self-assigned using variables names. Proposal Taking the syntax from [bpo-36817](https://bugs.python.org/issue36817) which just [made it to Python 3.8](https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging) the `=` syntax would be valid and have the the same effect as `=`, so these two statements would be equivalent: ```python foo(bar=bar, qux=qux) foo(bar=, qux=) ``` This syntax would only be valid inside functions calls so all of the following would remain invalid syntax: ```python x = 42 x= # SyntaxError ``` ```python x: Any [x= for x in foo] # SyntaxError ``` ```python x = 42 def foo(x=): ... # SyntaxError ``` The self-documenting variables in f-strings would also remain unchanged as it is not related to function calls: ```python x = "foo" f"{x=}" # x=foo ``` ```python x = "..." f"{foo(x=)=}" # foo(x=)=... ``` Also, this proposal does not intent to extend this syntax in any form to the dictionary literal declaration syntax. Although possible, language changes to support these are not being discussed here: ```python x = "..." foo["x"] = # just as stated previously, SyntaxError still ``` ```python x, y = 1, 2 foo = { x=, y=, } # SyntaxError ``` Worth noting that the following would now be possible with the proposed syntax: ```python x, y = 1, 2 foo = dict( x=, y=, ) ``` Rationale - Keyword arguments are useful for giving better semantics to parameters being passed onto a function and to lift the responsibility from the caller to know arguments order (and possibly avoid mismatching parameters due to wrong order). Said that, keyword and keyword-only arguments are extensively used across Python code and it is quite common to find code that forwards keyword parameters having to re-state keyword arguments names: ```python def foo(a, b, c): d = ... bar(a=a, b=b, c=c, d=d) ``` Of course if `bar` arguments are not keyword-only one could just omit keywords, i.e. `bar(a, b, c, d)`. Though now the caller has to be aware of arguments ordering again. And yet, if arguments are keyword-only then there is nothing you can do. Except for relying on varkwargs: ```python def foo(**kwargs): kwargs["d"] = ... bar(**kwargs) ``` Which, beyond having its own disadvantages, defeats its own purpose of avoiding repeating or reassigning the keys real quickly if one has to work with these arguments in `foo` before calling `bar`: ```python def foo(**kwargs): kwargs["d"] = kwargs["a"] * kwargs["b"] / kwargs["c"] bar(**kwargs) ``` With all that's being said, introducing self-assigned keywords would be useful for a wide range of Python users while being little intrusive on the Python syntax and backwards compatible. Best regards, Rodrigo Martins de Oliveira ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/SIMIOC7OW6QKLJOTHJJVNNBDSXDE2SGV/ Code of Conduct: http://python.org/psf/codeofconduct/