Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Can Pythonista be used to follow a book that uses Python 3
-
Hi,
I bought the book "Practical Programming: In Introduction to Computer Science using Python 3, 2nd ed. Since it uses Python 3, I was wondering if I could use Pythonista (as I have read it is for v2.7) to work through the book? I only ask because I will be on vacation and want to take my iPad and the book (not my macbook).Thanks
-
There are many differences between Python 2 and 3 - a few are quite obvious, but many aren't. This means that if you try to run normal Python 3 code on Python 2, you're likely to encounter some different behavior. In more complicated programs that can lead to errors. It is possible to write code that will run (almost) the same way on Python 2 and 3, but that requires knowledge of how both versions work and what exactly the differences are. It's certainly not something you should try if you're new to Python.
Instead I'd recommend installing Python 3 on a normal computer and going through the book with that. Once you know Python 3 well, have a look at the Python docs to find out what's different in Python 2. The two versions are not completely different languages, so if you know Python 3 then Python 2 code will not look like complete gibberish to you. The differences are mostly technical ones that aren't directly obvious, and differences in the standard library, because Python 3 is simply newer than Python 2.
-
Just put
from __future__ import division, print_function
at the top of your scripts, and I bet you'll be close enough to follow basic examples. Most good Python works in both 2 and 3 (That's especially true for good Python 3). If you come across
input
, rename that toraw_input
. You might want to grab a downloadable version of a Python 2/3 comparison (which there are plenty) if you run into odd errors.I've finally made all my work and personal scripts 2/3 compatible, and have switched to Python 3 everywhere except Pythonista. The new 3.5 version finally has some useful new tools, so I want to be ready for them. Still hoping eventually for a Pythonista 3! :)
-
"How to make your code Python 2/3 compatible" video from this year's PyCon: http://pyvideo.org/video/3427/how-to-make-your-code-python-23-compatible
In that video, Brett Cannon recommends this as the first import line:
from __future__ import (absolute_import, division, print_function, unicode_literals)
-
and I bet you'll be close enough to follow basic examples
Until you start working with classes, where the book will most likely not inherit from
object
explicitly. That is necessary on Python 2, else you get an old-style class, which only supports the most basic features of Python classes and objects.In that video, Brett Cannon recommends this as the first import line:
from future import (absolute_import, division, print_function, unicode_literals)
The `unicode_literals` part can be problematic by the way. On Python 2 attribute names are (8-bit) `str`, and on Python 3 they are (Unicode) `str`. If you import `unicode_literals`, to get a string that is usable on Python 2 and 3 in e. g. `getattr`, you need to use the ugly form `str("attr_name")`. Without `unicode_literals` you have three string literal types: `b"8-bit bytes"`, `"attribute name"`, and `u"unicode text"`. The first type is used for raw byte data, the second one for anything representing some kind of Python name, and the third one for text. Unless you absolutely need compatibility with Python 3.0 through 3.2, where `u"unicode text"` is a syntax error.
-
I've never had good luck with trying to import the Unicode literals in Python 2. It usually works better for me to leave that out, usually code still works on both without it. It tends to break libraries.
And, I very much doubt that an introductory computer science book would be covering anything that would be different with old-style classes, much less with the absolute import. For that matter, it might use the new style super, etc; those tend to be more advanced concepts.
To use new style classes: replace
class Something:
withclass Something(object):