如何在 python 中重命名命令 (print, os.<...>)?
How to rename commands (print, os.<...>) in python?
如何重命名 python 中的函数,例如将 print
重命名为 say
?
python 代码中的一些小改动,您可以将其放入模块(例如插件包)。
我不确定您为什么要重命名打印,但我会这样做。
对于python 3.X:
myvar = "Hello World"
say = print
say (myvar)
我的 Python 3.X 示例不适合 Python 2.X 除非其他人知道与我的示例类似的方法。否则,您可以通过以下方式为 Python 2.X
myvar = "Hello World"
def printFun(stuff):
print(stuff)
say = printFun
say (myvar) # note that like python 3 you must put this in ()
任何时候你想要"rename"一个函数,你需要做的就是将该函数分配给一个变量,然后将该变量用作函数。
编辑:在相关说明中,您还可以将 python 3 函数导入 python 2:
# this is good to use in 2.X to help future proof your code.
# for at least the print statement
from __future__ import print_function
myvar = 'Hello World'
say = print
say (myvar)
如何重命名 python 中的函数,例如将 print
重命名为 say
?
python 代码中的一些小改动,您可以将其放入模块(例如插件包)。
我不确定您为什么要重命名打印,但我会这样做。
对于python 3.X:
myvar = "Hello World"
say = print
say (myvar)
我的 Python 3.X 示例不适合 Python 2.X 除非其他人知道与我的示例类似的方法。否则,您可以通过以下方式为 Python 2.X
myvar = "Hello World"
def printFun(stuff):
print(stuff)
say = printFun
say (myvar) # note that like python 3 you must put this in ()
任何时候你想要"rename"一个函数,你需要做的就是将该函数分配给一个变量,然后将该变量用作函数。
编辑:在相关说明中,您还可以将 python 3 函数导入 python 2:
# this is good to use in 2.X to help future proof your code.
# for at least the print statement
from __future__ import print_function
myvar = 'Hello World'
say = print
say (myvar)