class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
try:
print p.name
print p.__score
except:
print "attributeerror"
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
try:
print p.name
print p.__score
except:
print "attributeerror"
2016-01-15
def performance(unit):
def wrapper(f):
def invoke(*args, **kw):
t = time.ctime()
print "call",f.__name__+"() in",t,unit
return f(*args, **kw)
return invoke
return wrapper
def wrapper(f):
def invoke(*args, **kw):
t = time.ctime()
print "call",f.__name__+"() in",t,unit
return f(*args, **kw)
return invoke
return wrapper
2016-01-15
最赞回答 / voipman
class Person: passxiaoming = Person()xiaohong = Person()print xiaomingprint xiaohong打印的内容如下:<__main__.Person instance at 0x00000000020B54C8><__main__.Person instance at 0x00000000021DAFC8>说明xiaoming和xiaohong是两个不同的实例。
2016-01-15
import math
def is_sqr(x):
return math.sqrt(x)%1 == 0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)%1 == 0
print filter(is_sqr, range(1, 101))
2016-01-15
def calc_prod(lst):
def prod(l=lst):
return reduce(lambda x, y: x * y, l)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod(l=lst):
return reduce(lambda x, y: x * y, l)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-01-15
print sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower)
2016-01-15
def decorator(text):def function(func):
@functools.wraps(func)
def wrapper(*args,**kw):
t1 = time.time()
print 'factorial values : %d' % func(*args,**kw)
t2 = time.time()
T= (t2-t1)*1000 if text == 'ms' else t2-t1
print 'call %s use time: %f%s' % (func.__name__,T,text)
return wrapper
return function
@functools.wraps(func)
def wrapper(*args,**kw):
t1 = time.time()
print 'factorial values : %d' % func(*args,**kw)
t2 = time.time()
T= (t2-t1)*1000 if text == 'ms' else t2-t1
print 'call %s use time: %f%s' % (func.__name__,T,text)
return wrapper
return function
2016-01-15
import time,functools
def performance(func):
@functools.wraps(func)
def wrapper(*args,**kw):
t1 = time.time()
print 'factorial: %s' % func(*args,**kw)
t2 = time.time()
print 'use time: %fs' % (t2-t1)
return
return wrapper
def performance(func):
@functools.wraps(func)
def wrapper(*args,**kw):
t1 = time.time()
print 'factorial: %s' % func(*args,**kw)
t2 = time.time()
print 'use time: %fs' % (t2-t1)
return
return wrapper
2016-01-15
import time
def performance(f):
def new_f(n):
print "call",f.__name__+"() in",time.ctime()
return f(n)
return new_f
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def new_f(n):
print "call",f.__name__+"() in",time.ctime()
return f(n)
return new_f
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2016-01-15
def cmp_ignore_case(s1, s2):
s1=s1.lower()
s2=s2.lower()
if s1>s2:
return 1
elif s1==s2:
return 0
else:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'],cmp_ignore_case)
s1=s1.lower()
s2=s2.lower()
if s1>s2:
return 1
elif s1==s2:
return 0
else:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'],cmp_ignore_case)
2016-01-14
import math
def is_sqr(x):
if math.sqrt(x).is_integer():
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x).is_integer():
return x
print filter(is_sqr, range(1, 101))
2016-01-14
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2016-01-14
def calc_prod(lst):
def prod():
return reduce(lambda r, x: r * x, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
return reduce(lambda r, x: r * x, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-01-14
def cmp_ignore_case(s1, s2):
return 1 if s1[:1].lower() > s2[:1].lower() else -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return 1 if s1[:1].lower() > s2[:1].lower() else -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-01-14
import math
def is_sqr(x):
return True if math.sqrt(x) % 1 == 0 else False
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return True if math.sqrt(x) % 1 == 0 else False
print filter(is_sqr, range(1, 101))
2016-01-14