[Tutor] Sort of database & "family tree" question

Timo timomlists at gmail.com
Wed Feb 4 12:34:19 CET 2009


Alan Gauld schreef:
> "spir" <denis.spir at free.fr> wrote
>
>>> > The current way of reading the data is this:
>>> > > def get_info(person)
>>> >    infoDic = {}
>>> >    infoDic['first']      = parser.get(person, 'firstName')
>>> >    infoDic['last']       = parser.get(person, 'lastName')
>>> >    infoDic['father']   = parser.get(person, 'father')
>>> >    infoDic['mother'] = parser.get(person, 'mother')
>>> >    return infoDic
>>>
>>> TYhis is almost the same but you are using a dict. A sligtly more 
>>> readable version is to define a class Person:
>>>
>>> class Person:
>>>      def __init__(self, parser):
>>>          self.first  = parser.get(person, 'firstName')

Hey Alan, thanks for that explanation!

But the class you gave, does almost the same thing as my function. We 
both store the info in a dic, so to retrieve info for a certain person, 
we both have to do the same thing. Or am I missing something here? If 
so, I would love to hear it! Because I have to admit that when I 
program, I rarely use classes.

>
> Yes, its not normal and the class probably should be called 
> ParsedPerson or somesuch. I ws trying to mimic the current code style 
> as much as possible, that was the reason for the
> decision. Normally I would agree with you and use:
>
>> class Person:
>>    def __init__(self, first, last, father, mother):
>>        self.first  = first
>>        self.last   = last
>>        self.father   = father
>>        self.mother = mother
>>


If I do it like this, I will have to parse all the data from the file 
and then send them to this class and then call this class again to 
retrieve it. Then I like my function better. But again, I would love to 
hear if I'm wrong.


Timo




More information about the Tutor mailing list