New Features In Python 3.x

I have updated the python's version from 2.7.x to 3.6 for pursing the effective work. But the python 3.x is not backwards imcompatible. So I decide the figure out what are the new fetures brought by python 3.x.

1 print is a function

print was replaced by print() function


In [1]:
print('The answer is ', 2*2)


The answer is  4

In [2]:
for x in range(5):
    print(x, end=' ')


0 1 2 3 4 

In [3]:
import sys
print('fatal error', file=sys.stderr)


fatal error

In [1]:
x, y = 1,2
print(x,y)


1 2

In [2]:
print((x,y))


(1, 2)

customize the separator between items


In [4]:
print('There are ', 2**32, ' possibilities!', sep='~')


There are ~4294967296~ possibilities!

2 Iterator Instead of Lists

2.1 dict method


In [7]:
dic = dict({'name':'gaofeng', 'age': 25, 'university':'cumt'})
print(type(dic.keys()))
print(type(dic.items()))
print(type(dic.values()))


<class 'dict_keys'>
<class 'dict_items'>
<class 'dict_values'>

In [8]:
for key in dic.keys():
    print(key, dic[key])


name gaofeng
age 25
university cumt

In [14]:
k = sorted(dic)
print(type(k))


<class 'list'>

dict.iterkeys(), ditc.iteritems() and dict.itervalues() methods are no longer supported.

remove the dict.has_key() method. Using in instead.


In [62]:
print('name' in dic)


True

2.2 map and filter

They return itertors no longer list


In [18]:
x = range(10)
type(map(lambda value:value**2, x))


Out[18]:
map

In [19]:
type(filter(lambda value:value%2==0, x))


Out[19]:
filter

2.3 range and zip

range behaves like xrange() which returns a iterator, so does zip function


In [22]:
type(range(10))


Out[22]:
range

3 Ordering Comparisons

When the operands don't have a meaningful natural ordering, the ordering comparsion operators will raise a TypeError instead of False


In [23]:
if 1 < '':
    pass


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-8523c450c7b8> in <module>()
----> 1 if 1 < '':
      2     pass

TypeError: '<' not supported between instances of 'int' and 'str'

Builtin.sorted() and list.sort() no longer accept cmp argument and __cmp()__ method does not support. Use __lt__() for sorting and __eq__() with __hash__() for other rich comparsion if you need.


In [31]:
class Person(object):
    def __init__(self, age):
        self._age = age
    @property
    def age(self):
        return self._age
    def __lt__(self, person):
        return self._age < person.age
    def __gt__(self, person):
        return self._age > person.age
    def __eq__(self, person):
        return self._age == person.age
persons = [Person(8), Person(4), Person(6)]
persons.sort()
for person in persons:
    print(person.age)


4
6
8

4 Divide


In [34]:
print(1/2)


0.5

In [35]:
print(1//2)


0

5 Text Vs. Data Instead of Unicode Vs. 8bit

5.1 Background Unicode

Before 1980s, almost all personal computer were 8-bit which means that one byte(8 bit) could represent value ranging from 0 to 255. And ASCII code only uses up to 127. As digits technology spreads all over the world, the short of characters emerged. One bytes could not map the whole the characters in the world. The Unicode standard describe how characters are represents by code points. Each code point is an integer value which are denoted in base 16(strared by '0X'). For example, we say 'This is character U+12CA'. U+12CA is a code point, which represents some particular character. You can go Unicode Query website to find its representive.

5.2 Encoding

Unicode string is a sequence of code points, which are number of from 0 to 0X10FFFF. But we want to map those sequence from code points to a set of bytes(value from 0 through 255) in memory. And the processing of translating a unicode string into a sequence of bytes are called an encoding.
But if ours translating is straightforward, which brings many weakness, such as importable, space-wasteful and imcompatible with some functions. Generally, we choose UTF-8 rule and the rules are listed below:

  • if the code point is < 128, each byte is the same as the value of the code point
  • if the code point is 128 or greater, it's turned into a sequence of two , three or four bytes, where each type of the sequence is between 128 and 255.

5.3 Python3 unicode support

Since python 3.x, the str type contains contains unicode characters, and the default encoding for Python source code is UTF-8. You can also put a specially-formatted comment as the first line of the source code if you want to change another encoding.


In [37]:
# -*- coding:<encoding name> -*-

You can also use Unicode character in identifiers


In [44]:
# -*- coding:utf8 -*-
高峰 = 'gaofeng'
for name in 高峰:
    print(name, end=',')


g,a,o,f,e,n,g,

You can use the character name and code point of Unicode character if you editor could not enter a particular character.


In [45]:
"\N{GREEK CAPITAL LETTER DELTA}"


Out[45]:
'Δ'

In [46]:
"\u0394"


Out[46]:
'Δ'

In [47]:
"\U00000394"


Out[47]:
'Δ'

You can create a string using the decode() method of bytes. This method method take an encoding argument, and optionally an error argument.


In [52]:
b"\x80abc".decode('utf-8','ignore')


Out[52]:
'abc'

str.encode() is opposite method of bytes.decode(), which returns a bytes represent of unicode string.


In [59]:
print(type('a'.encode('utf-8')))
for char in 'a'.encode('utf-8'):
    print(char)


<class 'bytes'>
97

In [61]:
bytes_value='高峰'.encode('utf-8')
bytes_value.decode('utf8')


Out[61]:
'高峰'

6 Other topic

6.1 input() method


In [63]:
guess = int(input('Enter an integer: '))


Enter an integer: 12

6.2 bin


In [64]:
bin(255)


Out[64]:
'0b11111111'

6.3 class topic

6.3.1 super() method


In [65]:
class C(object):
    def __init__(self, a):
        print('C', a)
class D(C):
    def __init__(self, a):
        super().__init__(a)
D(10)


C 10
Out[65]:
<__main__.D at 0x10f094470>

6.3.1 Class decorator


In [66]:
def foo(cls):
    def _wrapper(self):
        print('decorator')
    cls.print=_wrapper
    return cls
@foo
class Bar():
    pass
Bar().print()


decorator

6.3.2 Collections module


In [67]:
import collections
dir(collections)


Out[67]:
['AsyncGenerator',
 'AsyncIterable',
 'AsyncIterator',
 'Awaitable',
 'ByteString',
 'Callable',
 'ChainMap',
 'Collection',
 'Container',
 'Coroutine',
 'Counter',
 'Generator',
 'Hashable',
 'ItemsView',
 'Iterable',
 'Iterator',
 'KeysView',
 'Mapping',
 'MappingView',
 'MutableMapping',
 'MutableSequence',
 'MutableSet',
 'OrderedDict',
 'Reversible',
 'Sequence',
 'Set',
 'Sized',
 'UserDict',
 'UserList',
 'UserString',
 'ValuesView',
 '_Link',
 '_OrderedDictItemsView',
 '_OrderedDictKeysView',
 '_OrderedDictValuesView',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '_chain',
 '_class_template',
 '_collections_abc',
 '_count_elements',
 '_eq',
 '_field_template',
 '_heapq',
 '_iskeyword',
 '_itemgetter',
 '_proxy',
 '_recursive_repr',
 '_repeat',
 '_repr_template',
 '_starmap',
 '_sys',
 'abc',
 'defaultdict',
 'deque',
 'namedtuple']

6.4 next()

Itertor mode replaces next() method of __next__ method