[Tutor] modified dictionary class

Hugo Arts hugo.yoshi at gmail.com
Mon Dec 6 16:37:19 CET 2010


On Mon, Dec 6, 2010 at 4:32 PM, John <washakie at gmail.com> wrote:
> Hugo,
>
> Thank you for the response, it's very helpful. I posted a question
> earlier to this list (or possibly numpy) about how to create a 'matlab
> like structure'. This was early in my learning of Python, and yes, the
> intention was to have something that was similar to a matlab
> structure... now, I've been using Python much more, and I realize that
> this may be in fact 'un-Pythonic'. The problem is, I've used it
> extensively throughout my existing modules, and I have become somewhat
> comfortable with it...
>
> I guess an alternative would be to simply define a generic class, no?
>
> class Structure(object):
>    pass
>
> Then, you could easily say:
>
> S = Structure()
> S.this = ['my slice of cheese']
>
> and perhaps I would be being more 'pythonic'?? This I could quite
> easily do, as, where I have used this class, I almost never actually
> use the 'dictionary' like referencing...
>

This is better, and I believe it is even mentioned in the official
tutorial. A problem with this is that you don't get a lot of the nice
methods the dict class provides you with to iterate over its
keys/values, find keys, and more such things.

You *can* do it this way. But do you have a specific reason for *not*
using dicts? Because that would be the idiomatic way.


More information about the Tutor mailing list