On 26/04/18 14:48, Mats Wichmann wrote:

>>> However personally I'd use a class to define your data structure and
>>> just have a top level dictionary 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":

That's very true, its about personal preference mostly.

> that any more if there's not any clear benefit to using classes, and I
> don't see one for this problem. 

The advantage of using classes as defined data structures is reliability.

Instead of using strings as names of keys in a dict we get the
advantages of properly defined python attribute names so if
we spell it wrong we get a name error,  and we don't need to
surround everything with ['']. It's not infallible of course
because Python allows us to create attributes on instances at
runtime so spelling errors in assignments still result in
invalid fields being created. But at least we get a measure
of extra checking done by the interpreter and save some typing.

OTOH its definitely not good OOP, ther are no methods and we
are just using the class as a record. (A named tuple might
actually be a better option on reflection.)

> 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.

I'd also add the question of whether there might in fact be some
behaviour that these login records might acquire within the
system. For example formatted printing, comparisons between
records etc etc.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to