class Product(object):
def __init__(self, name, price):
self.name, self.price = name, price
def __add__(self, other):
return self.price + other.price
def __call__(self, *args, **kwargs):
return "PRODUCT: {} @ Tk {}".format(self.name, self.price)
a = Product("Apple", 160.00)
o = Product("Orange", 140.00)
assert a + o == 300
assert a() == "PRODUCT: Apple @ Tk 160.00"
# List
[i**2 for i in range(100)]
# A bit complex
[item for sublist in l for item in sublist]
# In two dimensions
[[c for c in line] for line in f]
# With guards
[i for i in range(100) if i%2]
# Works with set too
{i for i in [1,2,3,3,3,4,1,12,2,3,4]}
# Mathematics: f(x) = 2*x + 1
f = lambda x: 2*x + 1
is_odd = lambda x: not x%2
to_the_two = lambda limit: map(lambda x: x**2, range(limit)) # MAP
filter(is_odd, range(100)) # FILTER
reduce(lambda x,y: x+y, range(10)) # REDUCE
# Combine them all
import operator
reduce(operator.__add__, filter(is_odd, to_the_two(100)))
def bold_maker(f):
def _wrap(f, *args, **kwargs):
return "<b>{}</b>".format(f(*args, **kwargs))
return _wrap
@bold_maker
def greet(name):
return "Hello {}".format(name.capitalize())
# Alternatively
greet = bold_maker(lambda name: "Hello {}".format(name.capitalize())
# Making methods case insensitive
class Person(object):
def name(self):
return "<NAME>"
def age(self):
return "<AGE>"
def __getattr__(self, value):
for i in type(self).__dict__.keys():
if value.lower() == i.lower():
return getattr(self, i)
return "UNDEFINED"
p = Person()
"Name: {}; Age: {}".format(p.NaME(), p.AGE())
class Container(object):
def __init__(self, numbers):
self.numbers = numbers
def add(self, number):
self.numbers.append(number)
def __getattr__(self, attr):
if attr.startswith("get_"):
suffix = "_".join(attr.split("_")[1:])
if suffix == "odd_numbers":
return lambda: filter(lambda x: x%2 == 1, self.numbers)
if suffix == "less_than":
return lambda n: filter(lambda x: x < n, self.numbers)
c = Container(range(100))
c.get_odd_numbers(); c.get_less_than(50)
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!