[Tutor] Dict of Dict with lists

Mats Wichmann mats at wichmann.us
Thu Apr 26 09:48:49 EDT 2018


On 04/25/2018 12:46 PM, Kai Bojens wrote:
> On 25/04/2018 –– 18:35:30PM +0100, Alan Gauld via Tutor wrote:
>>> ...
>>>     for line in logfile:
>>>         result = pattern.search(line)
>  
>> Doesn't this overwrite your data structure?
>> I would strongly advise using another name.
> 
> You are of course right. I accidentally shortened this name as I was trying to
> fit my code into 80 characters width of this mail. That was sloppy ;) 
>  
>> However personally I'd use a class to define tyour data structure and
>> just have a top leveldictionary holding instances of the class.
> 
> You are right (again). I haven't thougt of using classes, but that's exactly
> what they were invented for. Thanks for pointing that out. 

Opinion follows - there's not a right-or-wrong on this, or, I think,
even a "most Pythonic":

Using a bunch of class instances stuffed into one of the built-in
iterable data structures will certainly work fine for this problem.
When I first started learning how to use the "OOP features", not coming
to Python from an OOP background, this is what I did - essentially, if I
would have coded a struct in C, I wrote a class in Python.  I don't do
that any more if there's not any clear benefit to using classes, and I
don't see one for this problem. As a learning excercise, you can try
writing it both ways and see how it feels to you.

In this case the dividing line might be... is this a building block for
some larger complex system where the clarity you get from the layout -
it's all there in the class definition - is important, or is this more
of a "throwaway" single purpose script.



More information about the Tutor mailing list