[Python-ideas] anonymous object support
Eric V. Smith
eric at trueblade.com
Mon Jul 25 20:08:33 CEST 2011
On 07/25/2011 12:51 PM, Georg Brandl wrote:
> 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 ;)
Yes, yes :). In my defense, moving the call into "mytype" was a last
minute change.
Eric.
More information about the Python-ideas
mailing list