0

I am missing the point why Python3 has commands that are not compatible with Python2.

For example the command

print 'hello'

does work in Python2 and not in Python3. Why?

I would expect Python3 to be compatible with Python2, the same as C#4 is compatible with C#2 for example.

user1813
  • 97
  • 2
  • 11
  • 2
    Why *do* you expect Python 3 to be compatible with Python 2? All Python 2.x series releases are compatible, but Python 3.x was a *major version change*, and is **not** compatible with 2.x. Within 3.x releases are compatible. – Martijn Pieters Sep 23 '13 at 06:41
  • 1
    The major version number changed **because** the language changed in incompatible ways. – Martijn Pieters Sep 23 '13 at 06:42
  • I expect the compatibility because I come from the C# world and for me it is normal to use C# 2.0 (delegates) features and C# 4.0 (dynamic) in the same program. For example I would like python3 to accept both print 'hello' and print('hello'), but perhaps this is not compatible with the philosophy of the language (?). – user1813 Sep 23 '13 at 06:49
  • Always do in Python 3 if you are starting new. – Santosh Kumar Sep 23 '13 at 06:56
  • 1
    @SantoshKumar Not necessarily. Not all modules are python 3 compatible – TerryA Sep 23 '13 at 07:05
  • See this question: http://stackoverflow.com/q/9066956/1931274 – pradyunsg Mar 04 '14 at 09:10

3 Answers3

8

I am missing the point why Python3 has commands are not compatible with Python2.

Because Python 3 is not the same language as Python 2.

Python releases normally are mostly backwards compatible with previous versions; Python 2.7 is largely backwards compatible with Python 2.6.

However, from the start, Python 3 (or 3000 as its design project originally codenamed) was specifically different. Quoting from one of the design documents:

Python 3000 will introduce a number of backwards-incompatible changes to Python, mainly to streamline the language and to remove some previous design mistakes.)

So, Python 3 is not backwards compatible to correct specific errors in the language that could not be corrected with backwards compatible changes.

The use of a statement to write to stdout instead of a function is one of those changes; print has been replaced by a function print().

Different language and software projects use different standards for what their version numbers mean. Python sticks to the major-minor-micro scheme; releases within the same major number are largely backwards compatible, releases within the same minor number only contain bug fixes. See the Python version number FAQ:

Python versions are numbered A.B.C or A.B. A is the major version number – it is only incremented for really major changes in the language. B is the minor version number, incremented for less earth-shattering changes. C is the micro-level – it is incremented for each bugfix release. See PEP 6 for more information about bugfix releases.

Python is also quite a bit older than C#. Python development started in 1989, and version 2.0 came out in 2000. C# on the other hand has only been around since 2002; perhaps in another decade or so it too will see a backwards-incompatible change.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Also, [PEP 3105](http://www.python.org/dev/peps/pep-3105/#rationale) lists five specific reasons for changing `print` to a function. – Janne Karila Sep 23 '13 at 07:07
3

Because in Python 2, print is a statement. But print() is now a function in Python 3. However, print(...) is still valid syntax in Python 2, and you can also do from __future__ import print_function to get Python 3's print function in Python 2.

Don't expect python 3 to be compatible with 2; there are many other changes. For example, reduce() was removed, and most built-in functions now return generators that once returned lists (eg map(), zip(), and filter()).

TerryA
  • 58,805
  • 11
  • 114
  • 143
0

python3 has a tool named "2to3". This tool will help you to convert python2.x source code to python3.x source code. Read the manual http://docs.python.org/2/library/2to3.html

Yarkee
  • 9,086
  • 5
  • 28
  • 29
  • 1
    While this is true... it doesn't answer the OPs question of why some changes to Python3 are not backwards compatible... – Jon Clements Sep 23 '13 at 07:05