[Python-ideas] anonymous object support

Devin Jeanpierre jeanpierreda at gmail.com
Thu Aug 4 06:39:43 CEST 2011


On Wed, Aug 3, 2011 at 11:49 PM, Herman Sheremetyev <herman at swebpage.com> wrote:
> 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.

Sure. Another stdlib-ish example is Sphinx, which does it in
sphinx.util.attrdict. And you can search google code for
'self.__dict__ = self', which reveals a particular(ly nasty) pattern
for it with many instances.

Devin

On Wed, Aug 3, 2011 at 11:49 PM, Herman Sheremetyev <herman at swebpage.com> wrote:
> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



More information about the Python-ideas mailing list