class Rational(object):
def __init__(self, p, q):
self.p = p
self.q = q
def __int__(self):
return self.p // self.q
def __float__(self):
return (self.p / float(self.q))
print float(Rational(7, 2))
print float(Rational(1, 3))
def __init__(self, p, q):
self.p = p
self.q = q
def __int__(self):
return self.p // self.q
def __float__(self):
return (self.p / float(self.q))
print float(Rational(7, 2))
print float(Rational(1, 3))
2015-03-23
import time
def performance(f):
def fn(*args, **kw):
t1 = time.time()
t2 = time.time()
print 'call %s() in %fs' % (f.__name__, (t2 - t1))
return f(*args, **kw)
return fn
def performance(f):
def fn(*args, **kw):
t1 = time.time()
t2 = time.time()
print 'call %s() in %fs' % (f.__name__, (t2 - t1))
return f(*args, **kw)
return fn
2015-03-23
class Person(object):
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k,v in kw.items():
setattr(self, k ,v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k,v in kw.items():
setattr(self, k ,v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2015-03-23
class Person(object):
count =0
def __init__(self, name):
self.name = name
Person.count +=1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
count =0
def __init__(self, name):
self.name = name
Person.count +=1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2015-03-22
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
print p.__score
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
print p.__score
2015-03-22
class Person(object):
def __init__(self, name, sex, birth, **kw):
self.name = name
self.sex = sex
self.birth = birth
for (k,v) in kw.items():
setattr(self, k, v)
...
def __init__(self, name, sex, birth, **kw):
self.name = name
self.sex = sex
self.birth = birth
for (k,v) in kw.items():
setattr(self, k, v)
...
2015-03-22
try:
import json as json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
import json as json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
2015-03-22
def calc_prod(lst):
def a():
return reduce(lambda x,y:x*y, lst)
return a
f = calc_prod([1, 2, 3, 4])
print f()
def a():
return reduce(lambda x,y:x*y, lst)
return a
f = calc_prod([1, 2, 3, 4])
print f()
2015-03-22
import time
def performance(f):
def fn(*args, **kw):
start = time.time()
try:
return f(*args, **kw)
finally:
print 'call {0}() in {1}s'.format(f.__name__, time.time() - start)
return fn
@performance
def factorial(n):
...
def performance(f):
def fn(*args, **kw):
start = time.time()
try:
return f(*args, **kw)
finally:
print 'call {0}() in {1}s'.format(f.__name__, time.time() - start)
return fn
@performance
def factorial(n):
...
2015-03-22
def count():
fs = []
for i in range(1, 4):
def f(i):
return lambda: i * i
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i):
return lambda: i * i
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-03-22
def calc_prod(lst):
return lambda: reduce(lambda x, y : x * y, lst)
f = calc_prod([1, 2, 3, 4])
print f()
return lambda: reduce(lambda x, y : x * y, lst)
f = calc_prod([1, 2, 3, 4])
print f()
2015-03-22
class Person(object):
def __init__(self, name, gender, **kw):
self.name=name
self.gender=gender
for key,value in kw.items():
setattr(self,key,value)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name=name
self.gender=gender
for key,value in kw.items():
setattr(self,key,value)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2015-03-22