leforestier / yattag

Python library to generate HTML or XML in a readable, concise and pythonic way.
333 stars 31 forks source link

Add support for all posible attributes which names are reserved words in Python #40

Closed nomad-vagabond closed 6 years ago

nomad-vagabond commented 6 years ago

Obviously, class is not the only reserved word in Python. Some attributes of html elements might have such names as for, in, with, etc.

To deal with all possible conflicts I propose to implement a common solution, based on adding underscore to the names of attributes, which are reserved words in Python.

I personally use for attribute in the label tag. Thus, with the proposed improvement I would specify _for in the definition of the tag, which will be processed by yattag as for.

leforestier commented 6 years ago

Hi,

you can use (key, value) pairs for that situation:

with tag('label', ('for', 'whatever')):
    ...

I think the (key, value) pair is the best solution each time you have to produce an XML attribute name that can't be expressed as a keyword argument in Python. Actually, I only keep the klass trick to avoid breaking people's code.

nomad-vagabond commented 6 years ago

Didn't know about (key, value) option. Thanks!

Still, the underscore shielding seems to be more elegant solution from my point of view. If you find it a useful feature I can come out with a pull request. By now I close the issue.

SeanFromIT commented 6 years ago

Can we call this out a little better in the tutorial?