Hacker News new | past | comments | ask | show | jobs | submit login

> Or, alternatively, "How many Python 2 scripts will run on a Python 3 interpreter?" Answer: "None of them."

That is completely false. So many libraries have support both for 2 and 3. That's code that run just as well under both interpreter.




Really? The same .py file runs under python2 and python3?

Googling quickly, I find this, which does a bit better than 2to3. I suppose one could write to a somewhat constrained intersection of Python2 and Python3, if one is willing to make at least some boilerplate changes to the original Python2 code.

https://python-future.org/overview.html

That said, if you bring a Python2 script and feed it to a Python3 interpreter, no, in general that will not work. They simply aren't the same language. Even a simple "print x" will do you in.


> The same .py file runs under python2 and python3?

Sure, as long as it doesn't contain any syntax or spellings which are incompatible between the two. That's a fairly large subset of the language.

> if you bring a Python2 script and feed it to a Python3 interpreter, no, in general that will not work. They simply aren't the same language. Even a simple "print x" will do you in.

But this will work:

    from __future__ import print_function
    print(x)
This is valid under both Python 2 and Python 3.

Also, as I said above, there is a pretty large subset of the Python language that has the same syntax and spellings in both Python 2 and Python 3, and any script or module or package that only uses that subset will run just fine under both interpreters. You are drastically underestimating both the size and the usage of this subset of the language.


Say Django 1.11, a massive amount of .py files, works completely fine under both 2 and 3. As do many other libraries.

Yes you often need some precautions like "from __future__ import" statements and sometimes libraries like `six`, but it's been perfectly normal practice for most of the last decade.


No, but it is possible to write Python 2 code than runs in Python 3.

In fact, the vast majority of popular libs had a 2/3 compatible code base for a few years.

The hard part was not the syntax in fact. It's pretty trivial: the language are not that different.

The hard part is the I/O stack, because the stdlib is very different, espacially for this part.


A lot of projects write in that style e.g. compatible with both python2 and python3, it's really common because there's so much py2 deployed (was default on centOS until very recently, still default on osx, etc.)

Nearly every py3 feature was backported to 2 you just need to write it in a compatible way. I'm seeing some drop py2 support now though. Which I'm fine with, I haven't written python2 code in maybe 6 or 7 years now.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: