News for hackers. Stuff that matters.
Re:The PEPs of Python 3.9 by downerending on Thursday May 28, 2020 @03:41PM (#23341617)
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.

Re:The PEPs of Python 3.9 by pdonis on Thursday May 28, 2020 @03:57PM (#23341829)
> 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.

Re:The PEPs of Python 3.9 by Scarblac on Thursday May 28, 2020 @03:53PM (#23341785)
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.

Re:The PEPs of Python 3.9 by BiteCode_dev on Thursday May 28, 2020 @03:55PM (#23341810)
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.

Re:The PEPs of Python 3.9 by 4ec0755f5522 on Thursday May 28, 2020 @04:52PM (#23342438)
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.