[Python-ideas] anonymous object support

Herman Sheremetyev herman at swebpage.com
Thu Aug 4 05:49:49 CEST 2011


On Thu, Aug 4, 2011 at 2:17 AM, dag.odenhall at gmail.com
<dag.odenhall at gmail.com> wrote:
>> obj = object(foo=1, bar=lambda x: x)
>> obj.foo
>>>>> 1
>> obj.bar(2)
>>>>> 2
>
> Problem? :)
>
>>>> from argparse import Namespace
>>>> obj = Namespace(foo=1, bar=lambda x: x)
>>>> obj.foo
> 1
>>>> obj.bar(2)
> 2

Good find, who'd have ever thought to look in argparse for something
like Namespace.. After some more poking around in the stdlib for
__init__ methods that take keyword args I found:

plistlib.Dict and plistlib.Plist

Both of them do the same thing as well as give access to the values
using [] notation. There are probably other examples in third party
libraries.

-Herman



More information about the Python-ideas mailing list