简体   繁体   English

for / while / print *东西*在Python中如何工作?

[英]How do the for / while / print *things* work in python?

What i mean is, how is the syntax defined, ie how can i make my own constructs like these? 我的意思是,语法是如何定义的,即我该如何构造自己的结构?

I realise in a lot of languages, things like this will be built into the compiler / spec, and so it's dealt with by the compiler (at least that how i understand it to work). 我意识到在很多语言中,类似的东西都将内置到编译器/规范中,因此由编译器处理(至少我理解它如何工作)。

But with python, everything i've come across so far has been accessible to the programmer, and so you more or less have the freedom to do whatever you want. 但是,使用python,到目前为止,我所遇到的一切都可以被程序员访问,因此您或多或少都可以自由地做自己想做的事情。

How would i go about writing my own version of for or while ? 我将如何编写forwhile自己的版本? Is it even possible? 可能吗

I don't have any actual application for this, so the answer to any WHY?! 我对此没有任何实际的应用,所以为什么要回答呢? questions is just "because why not?" 问题只是“因为为什么不?” or "curiosity". 或“好奇心”。

No, you can't, not from within Python. 不,您不能,不是在Python内部。 You can't add new syntax to the language. 您不能在语言中添加新的语法。 (You'd have to modify the source code of Python itself to make your own custom version of Python.) (您必须修改Python本身的源代码才能创建自己的自定义版本的Python。)

Note that the iterator protocol allows you to define objects that can be used with for in a custom way, which covers a lot of the possible use cases of writing your own iteration syntax. 请注意, 迭代器协议允许您以自定义方式定义可用于for对象,该对象涵盖了编写自己的迭代语法的许多可能的用例。

You can't make equivalent constructs. 您不能进行等效的构造。 for , while , if etc. are statements , and they are built into the language with their own specific syntax. forwhileif等都是statement ,它们使用自己的特定语法内置在语言中。 There are languages that do allow this sort of thing though (to some degree), such as Scala . 有些语言确实 (在某种程度上)允许这种事情,例如Scala

while , print , for etc. are keywords. whileprintfor等是关键字。 That means they are parsed by the python parser whilst reading the code, stripped any redundant characters and result in tokens. 这意味着它们会在读取代码的同时由python解析器进行解析,剥离所有多余的字符并生成令牌。 Afterwards a lexer takes those tokens as input and builds a program tree which is then excuted by the interpreter. 然后,词法分析器将这些标记用作输入,并构建程序树,然后由解释器执行。 Said so, those constructs are used only as syntactic sugar for underlying lexical machinery and as such are not visible from inside the code. 如此说来,这些构造仅用作底层词汇机器的语法糖,因此从代码内部看不到。

Well, you have a couple of options for creating your own syntax: 好了,您有两个选择来创建自己的语法:

  1. Write a higher-order function, like map or reduce . 编写一个高阶函数,例如mapreduce

  2. Modify python at the C level. 在C级别修改python。 This is, as you might expect, relatively easy as compared with fiddling with many other languages. 如您所料,这比起使用许多其他语言来说相对容易。 See this article for an example: http://eli.thegreenplace.net/2010/06/30/python-internals-adding-a-new-statement-to-python/ 参见本文的示例: http : //eli.thegreenplace.net/2010/06/30/python-internals-adding-a-new-statement-to-python/

  3. Fake it using the debug facilities, or the encodings facility. 使用调试工具或编码工具将其伪造。 See this code: http://entrian.com/goto/download.html and http://timhatch.com/projects/pybraces/ 参见以下代码: http : //entrian.com/goto/download.htmlhttp://timhatch.com/projects/pybraces/

  4. Use a preprocessor. 使用预处理器。 Here's one project that tries to make this easy: http://www.fiber-space.de/langscape/doc/index.html 这是一个试图简化这一过程的项目: http : //www.fiber-space.de/langscape/doc/index.html

  5. Use of the python facilities built in to achieve a similar effect (decorators, metaclasses, and the like). 使用内置的python工具来实现类似的效果(装饰器,元类等)。

Obviously, none of this is quite what you're looking for, but python, unlike smalltalk or lisp, isn't (necessarily) programmed in itself and guarantees to expose its own underlying execution and parsing mechanisms at runtime. 显然,这些都不是您要找的东西,但是与smalltalk或lisp不同,python本身不是(必需)编程的,并保证在运行时公开其自身的基础执行和解析机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM