wiki:Languages/Python3

Python 3.6+

Python 3.6+ is a new Lisp (without uniform prefix notation, macros and the code-is-data principle) and a new Smalltalk (without uniform message-passing-only syntax). It is well-designed around proper principles and highly polished by a devoted community. It has a culture, which improves the language and itself (a mutually recursive process).

Optional type annotations in Python 3.6+ and external type-checkers, such as mypy or pyre-check could make development process even more pleasant.

attention to details

Python is famous for attention to detail and doing everything just right (so does Go)

collections, iterators and generators (notice that everything is a class - an ADT):

>>> type("")
<class 'str'>
>>> type([])
<class 'list'>
>>> type(())
<class 'tuple'>
>>> type({})
<class 'dict'>

Comprehensions

type({x for x in ""})
<class 'set'>
>>> type([x for x in []])
<class 'list'>
type({x: x**2 for x in ()})
<class 'dict'>

iterators

>>> type(iter([x for x in []]))
<class 'list_iterator'>
>>> type(reversed([]))
<class 'list_reverseiterator'>
>>> type(enumerate({}))
<class 'enumerate'>

generator comprehensions

>>> type((x for x in ""))
<class 'generator'>

guards

>>> type((x for x in "" if x > 0))
<class 'generator'>

This will print nothing - the branch will never be executed

if None in []:
    print("yay!")
for a in []:
    print(type(a), a)

So, this code is wrong - explicit check for is not None is redundant

for user in db.query(User).filter_by(User.name == name):
    if user is not None:
        if email in user.addresses:
            return

The proper snippet is

for user in db.query(User).filter_by(User.name == name):
    if email in user.addresses:
        return

and this is beautiful code

It is the opposite of Languages/PHP

Last modified 8 months ago Last modified on Nov 19, 2018, 4:48:48 AM