tags:

views:

63

answers:

1

Hi.

I need to write XML files using lxml and Python.

However, I can't figure out whether to use a class to do this or a function. The point being, this is the first time I am developing a proper software and deciding where and why to use a class still seems mysterious.

I will illustrate my point.

For example, consider the following function based code I wrote for adding a subelement to a etree root.

from lxml import etree

root = etree.Element('document')

def createSubElement(text, tagText = ""):
    etree.SubElement(root, text)
    # How do I do this: element.text = tagText

createSubElement('firstChild')
createSubElement('SecondChild')

As expected, the output of this is:

<document>
  <firstChild/>
  <SecondChild/>
</document>

However as you can notice the comment, I have no idea how to do set the text variable using this approach.

Is using a class the only way to solve this? And if yes, can you give me some pointers on how to achieve this?

+1  A: 

The following code works:

def createSubElement(text, tagText = ""):
    elem = etree.SubElement(root, text)
    elem.text = tagText

createSubElement('firstChild', 'first one')
createSubElement('SecondChild', 'second one')

print etree.tostring(root)

Using a class rather than a function has mostly to do with keeping state in the class's instances (in very few use cases will a class make sense if there's no state-keeping required), which has nothing to do with your problem -- as the code shows, your problem was simply that you were not binding any name to the element returned from the SubElement call, and therefore of course you were unable to further manipulate that element (e.g. by setting its text attribute) in the rest of your function.

Alex Martelli
Oh wow, I wonder how I overlooked such a thing. Thank you. Do you think this approach is good or do I need to do this using a class?
sukhbir
@PulpFiction, if you want to keep per-instance state (for example because you may be handling more than one xml tree during the run), or in a few other pretty marginal cases even without per-instance state (if you want to use operator overloading, inheritance, and the like) then classes are a must. Otherwise, the normal Python style prefers functions -- a class that has no per-instance state (or at least falls in one of the other special cases above listed) is just boilerplate and overhead, so why bother?
Alex Martelli
I get it now. Thanks! I will be handling one tree per run but just adding sub elements to it. I just ran the code now and it works. Cheers!
sukhbir
@Pulp, great -- always glad to help.
Alex Martelli