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: Allow intermixing of keyword arguments and vargarg arguments
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.0, Python 3.1
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Imagist, LambertDW, amaury.forgeotdarc, ezio.melotti
Priority: normal Keywords:

Created on 2009-02-27 16:45 by Imagist, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg82839 - (view) Author: David Kerkeslager (Imagist) Date: 2009-02-27 16:45
This problem arose in this thread:
http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=11606

Basically, we have the following function which will generate an XHTML 
node:

def xhtmlNode(tag, *content, **attr):...

If we call:

xhtmlNode('div',id='sidebar','Hello, world')

... it should generate the xhtml:

<div id='sidebar'>Hello, world</div>

However, this isn't possible because the keyword argument isn't allowed 
to come before the 'vararg' argument.  We could do this:

xhtmlNode('div','Hello, world',id='sidebar')

... but this would not have symmetry with the generated xhtml and 
therefore complicates the code.  The solution, in my opinion, is to 
allow varargs to be intermixed with keyword args.  The above real-world 
example shows a use-case for this more flexible functionality.

If the following rules apply, there shouldn't be any issues:
1. Positional arguments must be in their position (positional arguments 
must come before all 'vararg' arguments and keyword arguments).
2. Varargs come in the order in which they are received, ignoring any 
keyword arguments that are intermixed.
3. Keyword arguments order doesn't matter (a dictionary isn't ordered).  
They can be intermixed with varargs.

Thus the following call:

xhtmlNode('div',id='sidebar',style='width:100px;float:left;','Hello,worl
d',xhtmlNode('p','Hello, world'))

... would result in the following html:

<div id='sidebar' style='width:100px;float:left;'> Hello, world 
<p>Hello, world</p></div>
msg82856 - (view) Author: David W. Lambert (LambertDW) Date: 2009-02-27 18:01
I think you need this order preserving paradigm using python as is: 

def xhtmlNode(tag,*args):
    ...

xhtmlNode('div', {'id':'sidebar'}, 'Hello world')


Less work in xhtmlNode might offset the extra work in writing the
function call.  Oh well, I didn't read the pythonforum thread.
msg82857 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2009-02-27 18:25
> xhtmlNode('div','Hello, world',id='sidebar')

> ... but this would not have symmetry with the generated xhtml and 
> therefore complicates the code.  The solution, in my opinion, is to 
> allow varargs to be intermixed with keyword args.  The above real-world 
> example shows a use-case for this more flexible functionality.

IMHO your API is confusing in the first place, if xhtmlNode is supposed
to create an XHTML element (possibly with attributes), why would you
want to pass also the content as a positional argument?

I would probably keep the element and its content separate and do
something like:
div = xhtmlNode('div', **attributes)
div.add(TextNode('Hello World'))

or, if you want a shortcut:
xhtmlNode('div', id='sidebar', content='Hello world') or
xhtmlNode('div', id='sidebar', text='Hello world')
msg89992 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-07-01 17:25
Also, the new syntax breaks the symmetry between the function definition
and the function call.

In any case, the issue tracker is the wrong place for such discussions.
This should be done on the python-ideas mailing list.
History
Date User Action Args
2022-04-11 14:56:46adminsetgithub: 49633
2009-07-01 17:25:15amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg89992

resolution: rejected
2009-02-27 18:25:08ezio.melottisetnosy: + ezio.melotti
messages: + msg82857
2009-02-27 18:01:28LambertDWsetnosy: + LambertDW
messages: + msg82856
2009-02-27 16:47:18Imagistsetcomponents: + Interpreter Core
2009-02-27 16:46:56Imagistsettype: enhancement
versions: + Python 3.0, Python 3.1
2009-02-27 16:45:48Imagistcreate