Python design patterns

Covering only a small portion of what exists.


In [19]:
import this as t

print(t)


<module 'this' from '/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/this.py'>

Explicit is better than implicit


In [23]:
fruits = ['apple', 'pear', 'cranberry']

# Good
for fruit in fruits:
    print(fruit)
    

# Bad
for i in fruits:
    print(i)
    
# okay
for index in range(10):
    print(index)
    
# Questionable
for i in range(10):
    for j in range(11):
        for k in range(11):
            pass


apple
pear
cranberry
apple
pear
cranberry
0
1
2
3
4
5
6
7
8
9

Simple is better than complex


In [26]:
class Person:
    
    def __init__(self, name):
        self.name = name
        self.is_hungry = True
        self.is_tired = False
    
    def __str__(self):
        return '{name} {id}'.format(
            name=self.name,
            id=id(self)
        )
    
    def fed(self, big_meal=False):
        self.is_hungry = False
        
        if big_meal:
            self.is_tired = True
    
    def sleep(self):
        self.is_hungry = True
        self.is_tired = False

bill = Person('Bill')
sammy = Person('Sammy')
jim = Person('Jim')
sue = Person('Sue')

persons = [bill, sammy, jim, sue]

jim.fed(True)
sue.fed(True)

# Bad check -- because it's complex and hides logic
if persons[0].is_hungry and persons[1].is_hungry and persons[2].is_tired and persons[3].is_tired:
    for person in persons:
        print('I have a person', person)


print()
print()
# Better check -- it doesn't hide meaning, but is verbose
def all_are_hungry(persons):
    for person in persons:
        if not person.is_hungry:
            return False
    return True

def all_are_tired(persons):
    for person in persons:
        if not person.is_tired:
            return False
    return True

hungry_people_group = persons[:2]
tired_people = persons[2:]

if all_are_hungry(hungry_people_group) and all_are_tired(tired_people):
    for person in persons:
        print('I have a person', person)

print()
print()

# Good/Best check -- doesn't hide meaning, and isn't verbose
has_hungry = all([person.is_hungry for person in persons[:2]])
has_tired = all([person.is_tired for person in persons[2:]])

if has_hungry and has_tired:
    for person in persons:
        print('I have a person', person)
        
print(range(10))


I have a person Bill 4576151368
I have a person Sammy 4576150752
I have a person Jim 4576150248
I have a person Sue 4576150864


I have a person Bill 4576151368
I have a person Sammy 4576150752
I have a person Jim 4576150248
I have a person Sue 4576150864


I have a person Bill 4576151368
I have a person Sammy 4576150752
I have a person Jim 4576150248
I have a person Sue 4576150864
range(0, 10)

Take a look at the main example in the folder

Below the main is imported and used. On the command line it is also use.


In [29]:
import echo

echo.main('I am testing my main function')


I am testing my main function
Out[29]:
0

In [30]:
import exponent

_ = exponent.main(2, 5)

_ = exponent.main(2, 5, quiet=True)
_ = exponent.main(2, 5, verbose=True)
_ = exponent.main(2, 5, quiet=True, verbose=True)


2^5 == 32
32
2 to the power 5 equals 32
32

In [ ]: