[Python-ideas] YAML (yet-another-multiline-lambda)
Steven D'Aprano
steve at pearwood.info
Tue Oct 22 20:07:29 CEST 2013
On Tue, Oct 22, 2013 at 09:27:59AM -0700, Andrew Barnert wrote:
> On Oct 22, 2013, at 5:39, Steven D'Aprano <steve at pearwood.info> wrote:
>
> > On Tue, Oct 22, 2013 at 03:10:29AM -0700, Antony Lee wrote:
> >
> >> Specifically, I suggest that the "def" (and possibly the "class")
> >> keyword(s) may be used in an expression context, if immediately surrounded
> >> by parentheses.
> >
> > I don't think there is any need to allow class in an expression, since
> > we already have type().
>
> But type doesn't allow you to do most of what you can do in a class definition. This is like arguing that we don't need expression def because we already have types.FunctionType.
Given multi-line lambda, what could you do in a class definition that
you couldn't do with type?
class Spam(SpamBase, HamBase):
x = 1
def eggs(self, arg):
return arg+self.x
classes = [int, str, Spam, float]
would become:
classes = [int, str,
type('Spam', (SpamBase, HamBase),
{'x': 1,
'eggs': (def eggs(self, arg):
return arg+self.x
),
}
),
float,
]
which I personally don't consider an improvement, but some people might.
You could even handle metaclasses and extra arguments:
class Spam(SpamBase, metaclass=MetaSpam, extrakw="extra"):
...
becomes:
MetaSpam('Spam', (SpamBase,), {...}, extrakw="extra")
so given a def expression, I don't think we also need a class
expression. What's missing?
--
Steven
More information about the Python-ideas
mailing list