passing keyword arguments that are themselfs python keywords

chris at onca.catsden.net chris at onca.catsden.net
Mon Feb 5 14:54:45 EST 2001


On Mon, 5 Feb 2001, Stefan Seefeld wrote:

> Hi,
>
> I'm trying to write a python function that can take keywords such as 'class'.
> Is that possible ?
>
> I'd like to be able to define something like:
>
> def entity(type, body, **attr):
>     print '<' + type + ' ' + string.join(map(lambda item:', %s="%s"'%item, attr.items())) + '>'
>     print body
>     print '</type>'
>
> and would like to call it like
>
> entity('div', text, class='header')
>
> Is there any way to achieve the desired effect ?

class is a python keyword, so the short answer is 'no'.

However, if you really /must/ use 'class' as a keyword, you could do
something like this (python-2.0 only)

entity ( 'div', test, **{'class':'header'} )


   ("`-/")_.-'"``-._        Ch'marr, a.k.a.
    . . `; -._    )-;-,_`)  Chris Cogdon <chmarr at furry.org.au>
   (v_,)'  _  )`-.\  ``-'
  _.- _..-_/ / ((.'       FC1.3: FFH3cmA+>++C++D++H++M++P++R++T+++WZ++Sm++
((,.-'   ((,/   fL               RLCT acl+++d++e+f+++h++i++++jp-sm++





More information about the Python-list mailing list