2017年10月9日月曜日

python2, 3の文法の違い

http://packtpub.ooops.me/2017/04/08/Mastering_matplotlib.pdf から転記


Syntactical Differences Python 2 Python 3
Division with floats x = 15 / 3.0 x = 15 / 3
Division with truncation x = 15 / 4 x = 15 // 4
Longs y = long(x * 10) y = int(x * 10)
Not equal x <> y x != y
The unicode function u = unicode(s) u = str(s)
Raw unicode u = ur"\t\s" u = r"\t\s"
Printing print x, y, z print(x, y, z)
Raw user input y = raw_input(x) y = input(x)
User input y = input(x) y = eval(input(x))
Formatting "%d %s" % (n, s) "{} {}".format(n,s)
Representation 'x' repr(x)
Function application apply(fn, args) fn(*args)
Filter itertools.ifilter filter
Map itertools.imap map
Zip itertools.izip zip
Range xrange range
Reduce reduce functools.reduce
Iteration iterator.next() next(iterator)
The execute code exec code exec(code)
The execute file execfile(file) exec(fh.read())
Exceptions try: ... except val, err: ... try: ... except val as err: ...


関数の属性名

Python2

function.func_name

Python3

function.__name__

0 件のコメント:

コメントを投稿