[Python-ideas] Syntax for easy binding __name__, __module__, __qualname__ to arbitrary objects
Haoyi Li
haoyi.sg at gmail.com
Mon May 13 03:27:07 CEST 2013
Since MacroPy came up, I thought I should chime in (since I made it)
Macros could pretty trivially give you any syntax that doesn't blow up in a
SyntaxError. that means "def x = y" is out of the question, unless someone
changes the python runtime's lexer/parser to make that work.
What we have right now:
@case
class MyName(foo, bar): pass
allows the class to have methods (put them in place of the `pass`, just
like any other class) and generally looks quite pretty. It would be ~50
lines of code to make a macro for:
@enum
class MyEnum(Red, White, Blue): pass
or:
@enum
class MyEnum(index):
Monday(0)
Tuesday(1)
' Wednesday(2)
...
I would argue that "solving the more general problem" is exactly what
macros are about (if you think Macros are sketchy, you should look at the
implementation of namedtuple!), but I'm sure many would disagree. I just
wanted to clarify what is possible.
-Haoyi
On Sun, May 12, 2013 at 8:44 PM, Martin Morrison <mm at ensoft.co.uk> wrote:
> On 9 May 2013, at 11:29, Piotr Duda <duda.piotr at gmail.com> wrote:
>
> > These also apply for other objects like NamedTuple or mentioned
> NamedValues.
>
> +1
> I really like this proposal.
>
> In one of my libraries, I actually went one step further with the frame
> hack and injected the resulting object into the parent namespace in an
> attempt to avoid the duplicated declaration of the name. The syntax was
> something like:
>
> namedtuple.MyName("foo", "bar")
>
> as a bare statement, which would inject "MyName" into the namespace (using
> the frame hack) as well as getting the right module name. This is obviously
> very, very ugly. :-)
>
> > To solve these problems I propose to add simple syntax that assigns
> > these attributes to arbitrary object:
> > def name = expression
>
> FWIW, this syntax looks the most obvious to me, as it clearly communicates
> both the assignment of the return value of the expression to the name, and
> the fact that name is a local definition (and thus likely to acquire
> additional properties).
>
> Cheers,
> Martin
>
> > other possible forms may be:
> > def name from expression
> > class name = expression
> > class name from expression
> > name := expression # new operator
> >
> >
> > which would be equivalent for:
> > _tmp = expression
> > _tmp.__name__ = 'name'
> > _tmp.__qualname__ = ... # corresponding qualname
> > _tmp.__module__ = __name__
> > # apply decorators if present
> > name = _tmp
> >
> > with new syntax declaring Enum will look like
> > def Animals = Enum('dog cat bird')
> >
> > as pointed by Larry it may be done using existing syntax in form:
> > @Enum('dog cat bird')
> > def Animals(): pass
> >
> > but it's ugly, and may by confusing.
> >
> >
> > Other examples:
> > def MyTuple = NamedTuple("a b c d")
> > def PI = NamedValue(3.1415926)
> >
> >
> > --
> > 闇に隠れた黒い力
> > 弱い心を操る
> > _______________________________________________
> > Python-ideas mailing list
> > Python-ideas at python.org
> > http://mail.python.org/mailman/listinfo/python-ideas
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130512/cc87b1dc/attachment.html>
More information about the Python-ideas
mailing list