how does IPython's ? operator actually work?

久未见 提交于 2019-12-02 02:17:37

问题


So i was thinking that in order to implement such a feature in a console application , where appending a question mark at the end of a function name will pour out it's doc string , i would have probably used a feature like metaclasses , where upon definition/import , i'd duplicate all the module member names and produce new ones just for typing out doc strings.

Then i noticed that you don't need actual parenthesis to call the helper functions and python doesn't actually allow you to put a question mark at the end of the function name anyway.... So is this done in python or am i just wasting my time trying to figure this out?

Thanks in advance


回答1:


It's not done the way you're imagining. ipython reads your command prompt input as a line of text, so it has a chance to check if it ends with a question mark before it passes it on to eval (or whatever). If it does, it runs help(name) instead of what you typed.

AST looks a little heavy-duty, but you can get a feel for how this works by checking out the module code. It gives you a lightweight interpreter that you can extend with syntax of this sort if you want.




回答2:


ipython uses AST, you can customize the syntax parsing and create a new ipython fork.

may help




回答3:


Have a look at the IPython.core.inputsplitter module for the code that parses the raw input line for things like ?, !, /, %, etc.



来源:https://stackoverflow.com/questions/9884764/how-does-ipythons-operator-actually-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!