[Python-ideas] anonymous object support

Nick Coghlan ncoghlan at gmail.com
Mon Jul 25 00:47:55 CEST 2011


On Mon, Jul 25, 2011 at 2:03 AM, Guido van Rossum <guido at python.org> wrote:
> Also, every one believes there own favorite feature to be so important
> that it's worth changing the language, but they don't want to grant
> that to anybody else's favorite feature... :-)

We've actually been down the 'namespace object' road years ago (Steve
Bethard even wrote a proto-PEP IIRC) and it suffered the same fate as
most enum PEPs: everyone has slightly different ideas on what should
be supported, so you end up being faced with one of two options:
1. Ignore some of the use cases (so some users still have to do their own thing)
2. Support all of the use cases by increasing the API complexity (so
many users will still do their own thing instead of learning the new
API)

In this space, collections.namedtuple is a well-executed variant of
the first alternative. There are some use cases it doesn't handle, but
it covers many of them and does it well.

For the OP's use case, I'll simply note that this functionality is
already provided by the type builtin:

obj = type('Foo', (), dict(foo=(lambda x: x)))()

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list