已采纳回答 / 小肥鱼2020
在Python中,定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用 return 语句返回。add是函数名,def是函数的定义语句add(25, 9, math.sqrt)是对上面函数的调用
2018-10-06
class Fib(object):
def __call__(self,v):
pre,curr,L= 0,1,[]
for i in range(v):
L.append(curr)
pre,curr = curr,pre+curr
return L
f = Fib()
print f(10)
def __call__(self,v):
pre,curr,L= 0,1,[]
for i in range(v):
L.append(curr)
pre,curr = curr,pre+curr
return L
f = Fib()
print f(10)
2018-10-06
class Person(object):
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name = name
Person.__count += 1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name = name
Person.__count += 1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2018-10-05
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1,key=lambda p: p.name)
print L2[0].name
print L2[1].name
print L2[2].name
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1,key=lambda p: p.name)
print L2[0].name
print L2[1].name
print L2[2].name
2018-10-05
class Person:
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming == xiaohong
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming == xiaohong
2018-10-05
import time
def performance(unit):
def per_decorator(f):
def fn(*args):
print('%s call...' % unit)
return f(*args)
return fn
return per_decorator
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10
def performance(unit):
def per_decorator(f):
def fn(*args):
print('%s call...' % unit)
return f(*args)
return fn
return per_decorator
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10
2018-10-04
import time
def performance(f):
def fn(x):
print("%4d-%02d-%02d %02d:%02d:%02d" % (time.localtime()[0:6]))
return f(x)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def fn(x):
print("%4d-%02d-%02d %02d:%02d:%02d" % (time.localtime()[0:6]))
return f(x)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2018-10-04
import time
def performance(f):
def fn(x):
print(time.asctime())
return f(x)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def fn(x):
print(time.asctime())
return f(x)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2018-10-04
已采纳回答 / qq_研_motJOQ
return lambda : i*i 返回的是一个函数,并把函数存进列表,分别传递给f1, f2,f3,之后的f1(),f2(), f3()表示调用返回的lambda函数,而去掉lambda后返回的是一个值(即 i*i的值),此时f1 ,f2, f3为数值,数值无法被当做函数调用(即不存在 1() 这种写法)
2018-10-04
j = 0
def count():
fs = []
for i in range(1,4):
def f():
global j
j += 1
return j*j
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
def count():
fs = []
for i in range(1,4):
def f():
global j
j += 1
return j*j
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-10-04
def calc_prod(lst):
def prod():
return reduce(lambda x,y: x*y,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
return reduce(lambda x,y: x*y,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-10-04
def cmp_ignore_case(s1, s2):
if s1[0].upper() > s2[0].upper():
return 1
if s1[0].upper() < s2[0].upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].upper() > s2[0].upper():
return 1
if s1[0].upper() < s2[0].upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-10-04
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])
2018-10-04
def format_name(s):
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-10-04