[Python-ideas] anonymous object support

Georg Brandl g.brandl at gmx.net
Mon Jul 25 18:51:46 CEST 2011


Am 25.07.2011 16:38, schrieb Eric V. Smith:
> On 07/25/2011 05:44 AM, Herman Sheremetyev wrote:
>> Giving those first two positional arguments default values (empty
>> string and empty tuple?) would make it even better:
>> 
>> obj = type(foo=lambda self, x: x)()
>> 
>> That's only one set of parens away from my initial proposal, but I
>> think it's close enough. Would there be any objections to changing the
>> type() API to support this?
> 
> -1 on changing type(). Just add this to your code:
> 
>>>> def mytype(**kwargs):
> ...   return type('', (), kwargs)()
> 
> Then you can get rid of the extra parens:
> 
>>>> obj=mytype(foo=lambda self, x: x)
>>>> obj.foo(3)
> 3

And if you call it anonymous_object() instead of mytype(), you have the
advantage of more readable code as well ;)

Georg




More information about the Python-ideas mailing list