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... --john On Mon, Dec 6, 2010 at 3:37 PM, Hugo Arts <hugo.yo...@gmail.com> wrote: > On Mon, Dec 6, 2010 at 2:47 PM, Alan Gauld <alan.ga...@btinternet.com> wrote: >> Oops, too quick. I neant to add: >> >> But this line seems to return a value so the return values of this function >> seem to be incompatible which is not a good design pattern. > > I agree it's not the clearest way to do this, but not because of > incompatible return values. Attribute and Method access have the same > syntax on objects, and __getattr__ is only called if the attribute > isn't found "in the usual places," so it won't hide other methods. In > other words: > >>>> a = Structure() >>>> a["spam"] = 5 >>>> a.__getstate__() > None >>>> a.spam > 5 > > Doesn't look that strange to me, interface-wise. Attribute access > sometimes returns a function and sometimes not. The comment > specifically states it's faking a method, so it's pretty clear. > > I do have a few other gripes though: > > * why not just define __getstate__ in the class? why return a new > lambda every time when you can just write "def __getstate__(self): > return None" and be done with it? This also simplifies getattr. > > * it's now impossible to have keys that collide with a method name: > dict, items, keys, clear, popitem, et cetera. You can set them without > error, but when you try to get them you'll get the old method, which > might cause hard-to-find bugs > > * it's not much of an advantage over regular dicts, really. You limit > yourself to string keys, you make it very unclear that you're using a > dict, it's not as efficient, and really just unpythonic in general. > Why do this at all? To make python more Matlab-like? > > Hugo > _______________________________________________ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Configuration `````````````````````````` Plone 2.5.3-final, CMF-1.6.4, Zope (Zope 2.9.7-final, python 2.4.4, linux2), Python 2.6 PIL 1.1.6 Mailman 2.1.9 Postfix 2.4.5 Procmail v3.22 2001/09/10 Basemap: 1.0 Matplotlib: 1.0.0 _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor