[Python-ideas] Proposal to add new built-in struct (was: Add kwargs to built-in function object)
Brett Cannon
brett at python.org
Fri May 23 01:00:11 CEST 2008
On Thu, May 22, 2008 at 3:24 PM, Brandon Mintern <bmintern at gmail.com> wrote:
> On Thu, May 22, 2008 at 6:10 PM, Brett Cannon <brett at python.org> wrote:
>> So you save three characters? I don't call that cumbersome.
>
> If foo["bar"] is not cumbersome, it is at least less elegant and the
> intent is less clear than foo.bar. Moreover, as I stated in the next
> paragraph, it does become cumbersome down the line when you decide
> that you should have used a class after all, and now you have to
> change all of those foo["bar"] lines to foo.bar. Note that simple
> search-and-replace wouldn't help if you are passing foo to various
> functions.
>
But you are suggesting that people think far enough ahead to think
that a sequence or mapping will be cumbersome and thus something with
attribute access should be used instead.
>> Thinking of a name for your class is not difficult, especially if you
>> keep it private to the module, class, function, etc.
>
> It may not be difficult, but when the name is unnecessary, simply
> needing to declare it seems silly.
>
Well, we almost ditched lambda and were going to require people to
define a simple function to replace lambda functions, so not everyone
thinks it is silly.
>> This does not strike me as useful enough to have as a built-in. It
>> would be better placed in the stdlib.
>
> I would be happy with it at least becoming part of collections or some
> other module, but then I wonder how many new-ish Python programmers
> would persist in using a tuple or a dict instead of a more elegant
> struct solution for lack of knowing about it. At least if it was in
> Python somewhere, though, searching for "python struct" would be more
> likely to return what the programmer is looking for.
>
Sticking something in built-ins so that it is easier for newbies to
find it is not a good argument. Things only go into builtins if they
are frequently used and warrant skipping an import statement.
> Ouch... it seems that struct is already the name of a module. If
> enough people like my idea, perhaps that module could be renamed to
> "cstruct". Then again, if my idea did become a part of collections
> (rather than a built-in), collections.struct and the struct module
> would be able to co-exist, albeit somewhat confusingly.
>
I don't agree with that worry. re.compile() exists and people don't
worry about conflicting with the built-in function. An import
statement makes it clear what object 'struct' maps to in the
namespace.
-Brett
More information about the Python-ideas
mailing list