How to use a Python keyword as a parameter?

Asked

Viewed 57 times

-4

How can I pass a parameter named from? The object ET.Element allows me to pass various parameters that will be attributes of my element in XML, but how to use a name that is a language keyword, such as the from? In doing from="A" gives syntax error.

root = ET.Element("root")

root.append(ET.Element('vType',id="F21a-17-00", color="0,0,255", begin="0" ,end= "900" ,period="900" ,type="automovel", departLane = "random" , from="A" ,to="b"))

tree = ET.ElementTree(root)
tree.write("filename.xml")

Makes the mistake:

Syntaxerror: invalid syntax

  • I hope that with the issue reverts this vote in question. I honestly do not understand why she deserved so many negative votes.

1 answer

2

You can define a dictionary with the desired values:

extra = {
    ...
    "from": "A",
    "to": "B"
}

And pass them as parameter by deconstructing the dictionary:

element = ET.Element('vType', **extra)

So you won’t be using the keyword as object name.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.