[Tutor] Dict of Dict with lists

Neil Cerutti neilc at norwich.edu
Thu Apr 26 13:09:38 EDT 2018


On 2018-04-26, Alan Gauld via Tutor <tutor at python.org> wrote:
> 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.)

namedtuple is great in lots of cases, but sometimes it transpires
I wish to make alterations in the data while I'm working.
Rebuilding tuples on the fly is possible, but painful. With a
class instance it is easy to use __slots__ to help prevent the
accidental creation of new attributes.

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

Excellent point! At the very least, such classes usually gain the
ability to build themselves with an __init__ method, which can be
a convenient place to put any logic they require.

-- 
Neil Cerutti



More information about the Tutor mailing list