[Python-ideas] anonymous object support
Eric V. Smith
eric at trueblade.com
Mon Jul 25 16:38:35 CEST 2011
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
Eric.
More information about the Python-ideas
mailing list