parsing XML

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Fri May 14 21:55:08 EDT 2010


In message <mailman.170.1273850586.32709.python-list at python.org>, Stefan 
Behnel wrote:

> Here's an overly complicated solution, but I thought that an object
> oriented design would help here.

How many times are you going to write the “"name", "age", "height"” 
sequence? The next assignment question I would ask is: how easy would it be 
to add a fourth attribute?

>    attributes = ['name', 'age', 'height']

I would put this at the top.

Then this

>    class Player(object):
>       def __init__(self, name, age, height):
>           self.name, self.age, self.height = name, age, height

can become

    class Player(object):
       def __init__(self, **rest):
           for attr in attributes :
               setattr(self, attr, rest[attr])
           #end for
       #end __init__
    #end Player

and

>    for player in players:
>        print player.name, player.age, player.height

can become

    for player in players:
        print " ".join(getattr(player, attr) for attr in attributes)
    #end for




More information about the Python-list mailing list