[Tutor] Creating an object which is an extension of a dictionary

Alan Gauld alan.gauld at btinternet.com
Sat Mar 26 13:50:06 EDT 2016


On 26/03/16 11:35, Ian Winstanley wrote:
> I'm attempting to create an object which can have elements which can be
> referenced like a dictionary but can also have normal methods and
> attributes like a conventional object but can also be referenced with key
> items using d[key]

I think you are looking for UserDict (in the UserDict module)
which is specifically designed with subclassing in mind.

> It isn't absolutely necessary but I would also like to validate when
> setting the dictionary attributes such as converting necessary values to
> boolean when setting and ensure the default case attribute is either UPPER
> or LOWER

You can do that by overriding one of the dunder methods.
I think __setitem__() is the one you need but the docs will
clarify.


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




More information about the Tutor mailing list