1

In this question - What's the difference between io.open() and os.open() on Python? - I learned that Python open() function is an alias to the io.open() function.

My question is how do I find out if one Python function aliases another one?

I thought id() function will help me out but in the case of open() and io.open() it returns different values:

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

I'm using Python 2.7.3

Community
  • 1
  • 1
golem
  • 1,820
  • 1
  • 20
  • 25
  • 1
    What is the actual problem you're trying to solve? – NPE Jan 18 '15 at 14:21
  • 1
    You're testing it in wrong Python version, try it in Python 3. – Ashwini Chaudhary Jan 18 '15 at 14:23
  • 1
    @NPE it's not always about solving a problem. In my case it's just learning. Going deeper helps me better understand it. – golem Jan 18 '15 at 14:26
  • 1
    @golem: That makes sense, and comparing the ids or using the `is` operator is the way to go. Bear in mind that there are many way for this check to break down (for example, a simple wrapper -- such as a lambda function -- would have a different `id`). – NPE Jan 18 '15 at 14:29

2 Answers2

4

In Python 3, the open() function is indeed the same object:

>>> 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

This is not the case in Python 2 however. The io module is available for forward compatibility, but the old I/O subsystem is still the default:

>>> 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

The io library was 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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

In Python 3.4,

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

In Python 2.x they are different objects, and you will get False instead.

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • Doesn't work in CPython 2.7.3 (and clearly not in the OP's case, where the two `id`s differ). – NPE Jan 18 '15 at 14:24
  • @NPE it does work - it's just that in 2.7 `open` doesn't use `io.open` – Jon Clements Jan 18 '15 at 14:24
  • @JonClements: I think that needs to be spelled out in the answer. – NPE Jan 18 '15 at 14:25
  • @NPE looks like Martijn's already covered it in his answer :) – Jon Clements Jan 18 '15 at 14:25
  • 1
    @JonClements: He did, and that's why his answer got my upvote. :-) That aside, I still can't shake off the feeling that there's a bit of an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) going on in this question. – NPE Jan 18 '15 at 14:27
  • @NPE, thanks for pointing out that such a notion exists. After some speculation I think that I indeed posed my question such that it looks like an XY Problem. I should have probably divided the question in two: 1) What is the correct way of finding out if one function is an alias of another; and 2) Are open() and io.open() functions truly the same thing. Anyway I've got my answers. – golem Jan 18 '15 at 15:30
  • @golem: As long as you got your answers, that's all that matters. It's just that we often see XY questions, and the whole thing turns into quite a frustrating experience for both the person asking and those trying to help. :-) – NPE Jan 18 '15 at 15:54