如何确定一个 Python 函数是否是另一个函数的别名?

How do I find out if one Python function is an alias for another one?

在这个问题中 - What's the difference between io.open() and os.open() on Python? - 我了解到 Python open() 函数是 io.open() 函数的别名。

我的问题是如何确定一个 Python 函数是否为另一个函数起别名?

我认为 id() 函数会帮助我,但在 open()io.open() 的情况下,它 returns 不同的值:

>>> import io
>>> id(open)
140172515129392
>>> id(io.open)
28340168

我正在使用 Python 2.7.3

在Python3.4中,

>>> import io
>>> open is io.open
True

在Python 2.x中它们是不同的对象,你会得到False

在Python3中,open()函数确实是同一个对象:

>>> sys.version
'3.4.2 (default, Nov 29 2014, 18:28:46) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)]'
>>> id(open)
4467734600
>>> id(io.open)
4467734600
>>> io.open is open
True

然而,不是 Python 2 中的情况。 io 模块可用于向前兼容,但旧的 I/O 子系统仍然是默认的:

>>> import sys, io
>>> sys.version
'2.7.8 (default, Nov 29 2014, 18:24:03) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)]'
>>> io.open is open
False

io 库是 added to Python 2.6:

In Python 2.6, the underlying implementations haven’t been restructured to build on top of the io module’s classes. The module is being provided to make it easier to write code that’s forward-compatible with 3.0, and to save developers the effort of writing their own implementations of buffering and text I/O.