Next / Previous / Contents / Shipman's homepage

9.20. Element.set(): Set an attribute value

To create or change an attribute named A to value V in an Element instance E, use this method:

E.set(A, V)

Here's an example.

>>> node = etree.Element('div', id='u401')
>>> etree.tostring(node)
'<div id="u401"/>'
>>> node.set('class', 'flyer')
>>> etree.tostring(node)
'<div id="u401" class="flyer"/>'
>>> node.set('class', 'broadside')
>>> etree.tostring(node)
'<div id="u401" class="broadside"/>'
>>> 

This method is one of two ways to create or change an attribute value. The other method is to store values into the .attrib dictionary of the Element instance.