...menustart
- Python3 Feature
- Feature 1: Advanced unpacking
- Feature 2: Keyword only arguments
- Feature 5: Everything is an iterator
- Feature 6: No more comparison of everything to everything
- Feature 7: yield from
- Feature 8: asyncio
- Feature 9: Standard library additions
- Feature 10: Fun
- Feature 11: Unicode and bytes
- Feature 12: Matrix Multiplication
- Feature 13: Pathlib
...menuend
>>> a, b, *rest = range(10)
>>> rest
[2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>> a, *rest, b = range(10)
>>> b
9
>>> rest
[1, 2, 3, 4, 5, 6, 7, 8]
>>>
>>> *rest, b = range(10)
>>> rest
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> b
9Get the first and last lines of a file
>>> with open("using_python_to_profit") as f:
... first, *_, last = f.readlines()Refactor your functions
def f(a, b, *args):
stuff
=>
def f(*args):
a, b, *args = args
stuffdef f(a, b, *args, option=True):
...- option comes after
*args. - The only way to access
optionis to explicitly callf(a, b, option=True) - You can write just a
*if you don't want to collect*args.
def f(a, b, *, option=True):
...def sum(a, b, biteme=False):
if biteme:
shutil.rmtree('/')3
else:
return a + b
>>> sum(1,2,3)
... Instead write
def sum(a, b, * , biteme=False):
if biteme:
shutil.rmtree('/')3
else:
return a + b
>>> sum(1, 2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sum() takes 2 positional arguments but 3 were given- Pretty great if you use generators
- Instead of writing
for i in gen():
yield i- Just write
yield from gen()- Easily refactor generators into subgenerators.
- Makes it easier to turn everything into a generator. See "Feature 5: Everything is an iterator" above for why you should do this.
- Instead of accumulating a list, just yield or yield from.
- for an example, we will create a list with dup numbers , like [0,0,1,1,2,2,3,3,...]
# bad
def dup(n):
A = []
for i in range(n):
A.extend([i, i])
return A
# good
def dup(n):
for i in range(n):
yield i
yield i
# better
def dup(n):
for i in range(n):
yield from [i, i]- faulthandler
- ipaddress
>>> ipaddress.ip_address('192.168.0.1')
IPv4Address('192.168.0.1')
>>> ipaddress.ip_address('2001:db8::')
IPv6Address('2001:db8::')- functools.lru_cache
- A LRU cache decorator for your functions.
@lru_cache(maxsize=32)
def get_pep(num):
...
>>> get_pep.cache_info()
CacheInfo(hits=3, misses=8, maxsize=32, currsize=8)- enum
- Finally, an enumerated type in the standard library.
>>> from enum import Enum
>>> class Color(Enum):
... red = 1
... green = 2
... blue = 3
...Function annotations
def f(a: stuff, b: stuff = 2) -> result:
...- Annotations can be arbitrary Python objects.
- Python doesn't do anything with the annotations other than put them in an
__annotations__dictionary.
- In Python 2, str acts like bytes of data.
- There is also unicode type to represent Unicode strings.
- In Python 3, str is a string.
- bytes are bytes.
- There is no unicode. str strings are Unicode.
np.dot(a, b)
#in python 3
a @ bfilepath = os.path.join(directory, "test_file.txt")
# in python 3
filepath = directory / "test_file.txt"