possible attribute-oriented class

Jan Kaliszewski zuo at chopin.edu.pl
Sat Sep 5 18:20:44 CEST 2009


05-09-2009 Steven D'Aprano <steve at remove-this-cybersource.com.au> wrote:

> On Fri, 04 Sep 2009 22:37:15 +0200, Jan Kaliszewski wrote:
>
>> Named tuples (which indeed are really very nice) are read-only, but the
>> approach they represent could (and IMHO should) be extended to some kind
>> of mutable objects.
[snip]
> What sort of extensions did you have in mind?

Two useful (from my point of view) concepts have appeared (or been linked
to) in this thread -- on python-list and python-ideas:

* the namespace/AttrDict concept (see Ken Newton's, Nick Coghlan's and my
   posts).

* record concept (see George Sakkis post).

>> The old discussion, the above link points to, shows that such a
>> dot-accessible dict-like class is something that many people need and
>> repeatedly implemet it (more or less perfectly) for themselves.
>
> I think it's something which people copy from other languages because
> that's what they're used to, not because they need it.

I don't think so, especially if we say about the former. IMHO it is simply
useful in practice, especially for scripting (but not only) -- being more
convenient than using empty class.

It offers (in compact way, without additional efforts and verbose
syntax -- once you have got such a tool implemented) three things at
the same time, without necessity to choose between them: comfortable
static attribute access, flexible dict-like dynamic access when needed
and possibility of iteration.

> It's just a change in syntax. Whether you write x.key or x['key'] is a
> matter of convenience. Attribute access is optimized for when you know
> the key names at compile time, key access is optimized for when you don't
> know the names until runtime.

Exactly. It is a matter of *convenience* (as well as large areas of Python)
and that's the point. I suppose that that is the reason for people to
repeatedly implement it for themselves.

05-09-2009 Steven D'Aprano <steve at remove-this-cybersource.com.au> wrote:

> On Fri, 04 Sep 2009 22:51:39 -0700, Ken Newton wrote:
[snip]
>> I would think this is much more than just copy from other language
>> styles or 'just' a syntax change -- the apparent widespread use would
>> hint at a deeper need.
>
> "Apparent" is the key word there. There are lots of people who *say* this
> this useful functionality, but how many of them *actually* use it? And of
> those who do use it, how many of them know what they're doing? There are
> an awful lot of bad programmers out there.
>
> If you do need such functionality, it's easy to implement. Here's one:

Neither you nor me have hard evidence about popularity/unpopularity of the
idea (number of places where you can find similar, more or less successful,
attempts to implement it seems to testify in favour of the idea) -- nor
about how it is used or abused.

Obviously there are a lot of bad programmers who are able to use globals
instead of function arguments etc.... Thats the fate of every language
feature.

But it's not the reason to resign from a feature that has particular common
and proper use-cases. Even official Python tutorial mentions a case that is
typical for the matter:

http://docs.python.org/3.1/tutorial/classes.html#odds-and-ends

> As a
> general rule, if obj.x is an attribute, then every valid obj should have
> an attribute x. But if obj['x'] is a key/value, then it is data-specific:
> some instances will have an 'x' key, and some won't.

It's often true but not always (see e.g. the above example in docs).

Cheers,
*j

-- 
Jan Kaliszewski (zuo) <zuo at chopin.edu.pl>



More information about the Python-list mailing list