python

Python 3.7: Syntax

print

print ist ab Python 3 eine Funktion:

# Python 2:
print "text"
print "text",
print "text1", "text2"
print >> sys.sdterr, "text"
# Python 3
print("text")
print("text", end=")
print("text1", "text2")
print("text", file=sys.stderr)

Mit einem from __future__ import print_function kann man die Umstellung auch schon zu Python 2-Zeiten machen.

Moved

  • raw_input()inpupt() – das alte input() aus Python 2 gibt es nicht mehr.
  • file()open()
  • apply()[func(_) for _ in iterable] oder ähnliches.
  • reduce()functools.reduce()
  • exec ist nun auch eine Funktion exec(code, globals, locals)
  • execfile()
  • reload()importlib.reload()
  • xrange()range()

Auch wurden viele Module aus der Standardbibliothek aufgeräumt und Verschoben. Entweder man verwendet six.moves oder so etwas wie

try:
 # Python 3
 from urllib.parse import urlparse, urlencode
 from urllib.request import urlopen, Request
 from urllib.error import HTTPError
except ImportError:
 # Python 2
 from urlparse import urlparse
 from urllib import urlencode
 from urllib2 import urlopen, Request, HTTPError
Written on March 11, 2020