在print曾经是在Python 2中的语句,如今却成了一个需要括号在Python 3的功能.
反正有没有在Python 3中抑制这些括号?也许通过重新定义打印功能?
所以,而不是
print ("Hello stack over flowers")
Run Code Online (Sandbox Code Playgroud)
我可以输入:
print "Hello stack over flowers"
Run Code Online (Sandbox Code Playgroud)
Tig*_*kT3 22
虽然您需要在Python 3中打印一对括号,但之后不再需要空格print,因为它是一个函数.所以这只是一个额外的角色.
如果您仍然发现键入一对括号"不必要地耗费时间",您可以这样做p = print并保存一些字符.因为您可以将新引用绑定到函数但不绑定到关键字,所以只能print在Python 3中执行此快捷方式.
Python 2:
>>> p = print
File "<stdin>", line 1
p = print
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
Python 3:
>>> p = print
>>> p('hello')
hello
Run Code Online (Sandbox Code Playgroud)
它会降低你的代码的可读性,但每次打印时你都会保存这几个字符.
mic*_*hau 10
print在 Python 3 代码中不使用括号不是一个好主意。创建别名等也不是。如果这是一个交易破坏者,请使用 Python 2。
但是,print不带括号在交互式 shell 中可能很有用。这实际上不是减少字符数的问题,而是避免在调试时每次要打印某些内容时都需要按两次 Shift 键。如果以斜线开头,IPython允许您在不使用括号的情况下调用函数:
Python 3.6.6 (default, Jun 28 2018, 05:43:53)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: var = 'Hello world'
In [2]: /print var
Hello world
Run Code Online (Sandbox Code Playgroud)
如果您打开autocall,您甚至不需要键入斜杠:
In [3]: %autocall
Automatic calling is: Smart
In [4]: print var
------> print(var)
Hello world
Run Code Online (Sandbox Code Playgroud)
小智 9
使用 Autohotkey 制作宏。AHK 是免费的,而且安装起来非常简单。www.autohotkey.com
您可以将宏分配给,例如,alt-p:
!p::send print(){Left}
Run Code Online (Sandbox Code Playgroud)
这将使 alt-p 输出 print() 并将光标移动到括号内。
或者,更好的是,为了直接解决您的问题,您可以定义一个自动替换并将其范围限制为打开的文件具有 .py 扩展名时:
#IfWinActive .py ;;; scope limiter
:b*:print ::print(){Left} ;;; I forget what b* does. The rest should be clear
#IfWinActive ;;; remove the scope limitation
Run Code Online (Sandbox Code Playgroud)
这是一个有保证的、无痛的、透明的解决方案。
不。这在 Python 3 中始终是一个语法错误。考虑使用2to3将代码转换为 Python 3
您可以使用运算符重载:
class Print:
def __lt__(self, thing):
print(thing)
p = Print()
p < 'hello' # -> hello
Run Code Online (Sandbox Code Playgroud)
或者,如果您喜欢<<像 c++ iostreams 中那样使用。
| 归档时间: |
|
| 查看次数: |
78847 次 |
| 最近记录: |