无法理解有关自定义Python宏语法的声明

时间:2023-01-06 00:05:50

Cody has been building a Pythonic Macro Syntax. He says

Cody一直在构建一个Pythonic宏语法。他说

These macros allow you to define completely custom syntax, from new constructs to new operators. There's no facility for doing this in Python as it stands.

这些宏允许您定义完全自定义语法,从新构造到新运算符。在Python中没有任何设施可以实现这一点。

I am not sure what he means by

我不确定他的意思

  • new constructs to new operators: Does he refer to binary operators such as +, - and multiplication in Math?
  • 新构造到新运算符:他是否引用二元运算符,如+, - 和Math中的乘法?

  • his main goal: Where do you benefit in customizing Python's macro syntax?
  • 他的主要目标:您在定制Python的宏语法方面受益匪浅?

1 个解决方案

#1


No doubt Cody refers to completely new operators that are not currently in Python, such as (I dunno) ^^ or ++ or +* and so on, whatever they might mean. And he's explicitly saying that the macro system lets you define a completely new syntax for Python (his question was about the syntax of the macro definitions themselves).

毫无疑问,Cody指的是目前还不是Python的全新运算符,例如(我不知道)^^或++或+ *等等,无论它们是什么意思。他明确地说宏系统允许你为Python定义一个全新的语法(他的问题是关于宏定义本身的语法)。

Some people care burningly about syntax and for example would much prefer to see Python uses braces rather than group by indentation; but Python itself will never follow those people's preferences...:

有些人非常关心语法,例如,我更倾向于看到Python使用大括号而不是按缩进分组;但Python本身永远不会遵循这些人的偏好......:

>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chance

So these people might obtain what they crave by defining a completely new syntax for Python through this macro system.

因此,通过这个宏系统为Python定义一个全新的语法,这些人可能会获得他们渴望的东西。

Others might use it to define specific custom languages that mostly follow Python's general outlines but add special new keywords, let you call functions without using parentheses, and so on, and so forth.

其他人可能会使用它来定义特定的自定义语言,这些语言大多遵循Python的一般轮廓,但添加特殊的新关键字,让您在不使用括号的情况下调用函数,依此类推。

Whether that's a good thing, in fact, is an ancient, moot issue—but some languages such as Lisp have always had macros of such power, and many people who came to Python from Lisp, such as Peter Norvig, would probably be quite happy to get back that syntax-making power they used to have in Lisp but lack in Python.

事实上,这是一件好事,是一个古老的,没有实际意义的问题 - 但是像Lisp这样的一些语言总是有这种力量的宏,许多从Lisp来到Python的人,比如Peter Norvig,可能会很开心恢复他们曾经在Lisp中使用的语法制作能力但缺乏Python。

#1


No doubt Cody refers to completely new operators that are not currently in Python, such as (I dunno) ^^ or ++ or +* and so on, whatever they might mean. And he's explicitly saying that the macro system lets you define a completely new syntax for Python (his question was about the syntax of the macro definitions themselves).

毫无疑问,Cody指的是目前还不是Python的全新运算符,例如(我不知道)^^或++或+ *等等,无论它们是什么意思。他明确地说宏系统允许你为Python定义一个全新的语法(他的问题是关于宏定义本身的语法)。

Some people care burningly about syntax and for example would much prefer to see Python uses braces rather than group by indentation; but Python itself will never follow those people's preferences...:

有些人非常关心语法,例如,我更倾向于看到Python使用大括号而不是按缩进分组;但Python本身永远不会遵循这些人的偏好......:

>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chance

So these people might obtain what they crave by defining a completely new syntax for Python through this macro system.

因此,通过这个宏系统为Python定义一个全新的语法,这些人可能会获得他们渴望的东西。

Others might use it to define specific custom languages that mostly follow Python's general outlines but add special new keywords, let you call functions without using parentheses, and so on, and so forth.

其他人可能会使用它来定义特定的自定义语言,这些语言大多遵循Python的一般轮廓,但添加特殊的新关键字,让您在不使用括号的情况下调用函数,依此类推。

Whether that's a good thing, in fact, is an ancient, moot issue—but some languages such as Lisp have always had macros of such power, and many people who came to Python from Lisp, such as Peter Norvig, would probably be quite happy to get back that syntax-making power they used to have in Lisp but lack in Python.

事实上,这是一件好事,是一个古老的,没有实际意义的问题 - 但是像Lisp这样的一些语言总是有这种力量的宏,许多从Lisp来到Python的人,比如Peter Norvig,可能会很开心恢复他们曾经在Lisp中使用的语法制作能力但缺乏Python。