Covering only a small portion of what exists.
import this
main
functions
In [19]:
import this as t
print(t)
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
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))
In [29]:
import echo
echo.main('I am testing my main function')
Out[29]:
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)
In [ ]: