def performance(f):
def prr(x):
print 'clever'
return f(x)
return prr
@performance
def f(x):
return x**2
print f(3)
#输出结果是clever 9
想用@简化装饰器,则装饰器必须在该函数之前定义,且用@简化装饰器后,原始函数的定义就被彻底隐藏了,只能拿到装饰后的函数
def prr(x):
print 'clever'
return f(x)
return prr
@performance
def f(x):
return x**2
print f(3)
#输出结果是clever 9
想用@简化装饰器,则装饰器必须在该函数之前定义,且用@简化装饰器后,原始函数的定义就被彻底隐藏了,只能拿到装饰后的函数
2017-09-25
@new_fn
def p(x):
return x+20
print p(1)
#简化装饰器调用,等价于下面的代码:
def p(x):
return x+20
p=new_fn(p)
print p(1)
def p(x):
return x+20
print p(1)
#简化装饰器调用,等价于下面的代码:
def p(x):
return x+20
p=new_fn(p)
print p(1)
2017-09-25
def format_name(s):
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2017-09-25
except AttributeError:
print 'AttributeError'
print 'AttributeError'
2017-09-25
def format_name(s):
return (s.strip()).capitalize();
print map(format_name, [' adam', ' LISA ', ' barT '])
return (s.strip()).capitalize();
print map(format_name, [' adam', ' LISA ', ' barT '])
2017-09-25
import math
def is_sqr(x):
for i in range(11):
if(x==i*i):
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
for i in range(11):
if(x==i*i):
return x
print filter(is_sqr, range(1, 101))
2017-09-24
def is_sqr(x):
return (x ** 0.5).is_integer()
print filter(is_sqr, range(1, 101))
return (x ** 0.5).is_integer()
print filter(is_sqr, range(1, 101))
2017-09-24
from __future__ import unicode_literals
s = 'am I an unicode?'
print isinstance(s, unicode)
注意这里的future下划线为两个
s = 'am I an unicode?'
print isinstance(s, unicode)
注意这里的future下划线为两个
2017-09-24