[Tutor] Identifying V3 examples
Steven D'Aprano
steve at pearwood.info
Tue Jul 21 05:05:27 CEST 2015
On Mon, Jul 20, 2015 at 10:53:32AM -0400, Jon Paris wrote:
> I’m having problems identifying sites that feature V3 code. My
> learning is being hampered by having to worry about conversion for the
> vast majority of the examples I encounter.
>
> Any suggestions on how to deal with this?
This can be an issue for beginners, so I'm sure you are not alone. But
remember, Python 2 and Python 3 share about 98% of the language, the
differences are quite small.
The most general way to deal with this, in my opinion, is to have both
Python 2 and Python 3 installed, and try the example in both and see
which one it works in.
The most obvious hint that you're using Python 3 is the use of print()
with round brackets (parentheses), that is, print as a function:
print x # Works in v2, syntax error in v3
print(x) # Likely to be v3
The second most obvious hint is the use of Unicode ("funny non-ASCII
characters") as ordinary strings, without the u prefix:
s = u"ßŮƕΩжḜ※€ℕ∞⌘⑃☃だ" # Probably v2
s = "ßŮƕΩжḜ※€ℕ∞⌘⑃☃だ" # Probably v3
I say "probably" because, starting with version 3.3, Python 3 also
supports the u"..." format, to make it easier to port code from v2 to
v3.
There are a few other changes, like the use of x.next() changing to
next(x), some changes in behaviour, some libraries were renamed for
consistency with the rest of the standard library, some functions were
moved around, etc. If you google for "Python 2 3 changes", you will find
plenty of places talking about this:
https://duckduckgo.com/html/?q=python+2+3+changes
https://startpage.com/do/search?q=python+2+3+changes
If in doubt, feel free to ask here!
--
Steve
More information about the Tutor
mailing list