How to use a "class" word as a function call in python

I am writing an XML generator for my manager request. For smaller printers, I decided to use ElementTree as a parser and SimpleXMLWriter as a writer.

The XML result requires attributes named "class". eg

<Node class="oops"></Node>

As stated in the official guide, to write an XML node, just use this method:

w.element("meta", name="generator", value="my application 1.0")

So I wrote:

w.element("Node", class="oops")

python fails with SyntaxError syntax. Any help?

+3
source share
3 answers

I think the SimpleXMLWriter developers had in mind this solution:

w.element("Node", None, {'class': 'oops'})

or

w.element("Node", attrib={'class': 'oops'})
+7
source

, , . , .

, "", :

w.element("Node", **{'class': 'oops'})
+5

classis a reserved word in Python. You simply cannot use it for a variable name, more than you can have a variable with a name classin C ++.

The usual abbreviation for classis either klassor cls.

Here is the official list of reserved words in Python:

http://docs.python.org/reference/lexical_analysis.html#keywords

+3
source

Source: https://habr.com/ru/post/1728420/


All Articles