Question: Is Python 3 Fun? Does it have the same "feel" as Python 2? Can you solve problems with it? Can you make things with it? Is it something that you want to use?
Using Python 3 Have been using Python 3 for various personal projects since about 2010 Trying to use it for all new projects However, mostly just playing around
After Sets A lot of code gets simplified a = set(['Guido', 'Barry', 'Tim', 'Paula']) b = set(['Dave', 'Paula', 'Thomas', 'Lewis']) all = a | b in_common = a & b a_not_b = a - b
Sets and Dicts There is a close relationship Sets : Unordered collection of items Dicts : Unordered set of keys mapped to values Underlying implementation almost identical
Python 3 In a Nutshell It's a more polished Python All of the parts just fit together better Think of it as a more finely tuned machine Made possible by breaking backwards compat
Exception Chains Traceback (most recent call last): File "", line 2, in ValueError: Must be > 0 The above exception was the direct cause of the following exception: Traceback (most recent call last): File "", line 4, in RuntimeError: Failed
C Interoperability Standardized interface for exposing memory regions and arrays (memoryviews) Gets array processing libraries (e.g., numpy), I/O libraries, and related tools to play nice
Unicode/Bytes It's a huge headache (still) Rules of thumb: All text input must be decoded All text output must be encoded Python 3 forces you to be precise about it There are semantic differences
Serialization Woes >>> s = "Hello" >>> json.dumps(s) '"Hello"' >>> pickle.dumps(s) b'\x80\x03X\x05\x00\x00\x00Helloq\x00. >>> Not entirely consistent across libraries Text Bytes Frankly, a mess
Unicode Insanity >>> f = open('jalape\xf1o.txt', 'w') >>> g = open(b'jalape\xf1o.txt', 'w') >>> import os >>> os.listdir('.') ['jalape%F1o.txt', 'jalapeño.txt'] >>> os.listdir(b'.') [b'jalape%F1o.txt', b'jalapen\xcc\x83o.txt >>> Really crazy things in system calls
Error Messages >>> data = b"Hello World" >>> 'Hell' in data Traceback (most recent call last): File "", line 1, in TypeError: Type str doesn't support the buffer API >>> WhAt?!?
What to Make of Python 3? Overall, a really nice language Better appreciated by doing new projects Especially if you're unchained from past Porting still a potential problem (sigh)
Software import serial ser = serial.Serial( '/dev/tty.usbmodem641', 9600) def command(cmd): ser.send(cmd.encode('ascii')+b'\n') resp = ser.readline() if resp != b'Ok\n': raise RuntimeError(resp) It's just serial ports... use pyserial Simple command/response protocol