This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: keywords in keyword_arguments not possible
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: cito, rhettinger
Priority: normal Keywords:

Created on 2005-02-01 13:47 by cito, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg24121 - (view) Author: Christoph Zwerschke (cito) * Date: 2005-02-01 13:47
Currently it is not possible to use keywords in
expressions of the type "keyword_arguments". Example:

d  = {'fi': 1, 'if': 1}

d.update(fi = 2) # ok
d.update(if = 2) # error

If possible, this restriction should be removed.
msg24122 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-02-01 17:46
Logged In: YES 
user_id=80475

It's possible, but hard to implement and unwise in any case.
msg24123 - (view) Author: Christoph Zwerschke (cito) * Date: 2005-02-01 18:49
Logged In: YES 
user_id=193957

Maybe I should explain my motivation for making this request.

In Kid [http://kid-template.sf.net], a xml templating
language, it is possible to set xml attributes with a
Pythonic syntax, like this:

<p py:attrs="class=myclass, bgcolor=mycolor, id=173" />

This expression is dynamically evaluated. If
myclass="aclass" and bgcolor="blue", then this would produce
the following:

<p class="aclass" bgcolor="blue" id="173">

Internally, this is of course realized by creating a
dictionary that contains the attributes of the tag, and then
creating an update statement with the string in py:attrs as
argument.

The problem arises when one of the attributes is a Python
keyword, "class" being a particularly embarassing example.

My current solution is to parse the py:attrs expression
manually and mangle the names. If Python could handle Python
keywords here, it would be a whole lot easier and faster.
msg24124 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-02-01 20:13
Logged In: YES 
user_id=80475

I have no doubt that there are good use cases, your included.

The two issues are that it would complicate the snot out of
parsing (not a good thing) and that allowing keywords in
non-keyword roles would throw-off many existing tools (esp.
syntax highlighters).  

In addition, human eye parsing also suffers when keywords
can be used in non-keyword roles.  IOW, readability suffers.

For your app, it is a reasonable solution to wrap your code
with in preprocessing that converts keywords to non-keywords:

    s = s.replace('class=', '_class=')
msg24125 - (view) Author: Christoph Zwerschke (cito) * Date: 2005-02-01 21:18
Logged In: YES 
user_id=193957

Your argument about syntax highlighters is valid.

s.replace('class=', '_class=') will not suffice, since
'class="aclass"' would be translated to 'class_=aclass_'
which would fail because aclass_ is not defined.

The only solution for me is to carefully parse the
keyword_argument string using  the tokenize module.
History
Date User Action Args
2022-04-11 14:56:09adminsetgithub: 41512
2005-02-01 13:47:26citocreate