class Fib(object):
def __init__(self):
pass
def __call__(self, number):
fib = []
a, b = 0, 1
for i in range(number):
fib.append(a)
a, b = b, a+b
return fib
f = Fib()
print f(10)
def __init__(self):
pass
def __call__(self, number):
fib = []
a, b = 0, 1
for i in range(number):
fib.append(a)
a, b = b, a+b
return fib
f = Fib()
print f(10)
2018-02-05
最新回答 / 慕运维413331
class Person(object): __count = 0 def __init__(self, name = ''): self.name = name Person.__count += 1 @classmethod def how_many(cls): return cls.__count
2018-02-05
最赞回答 / 幕布斯9506188
<...code...>运行该语句应该会返回以上错误,因为**kw指的是key word,也就是关键字参数,类似于字典,也就是需要传入如下类型的参数,程序将会运行正常<...code...>
2018-02-04
class Person(object):
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaohong == xiaoming
结果为:
<__main__.Person object at 0x7f73bd2c6450>
<__main__.Person object at 0x7f73bd205ad0>
False
(可以用is代替==进行两个实例的比较)
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaohong == xiaoming
结果为:
<__main__.Person object at 0x7f73bd2c6450>
<__main__.Person object at 0x7f73bd205ad0>
False
(可以用is代替==进行两个实例的比较)
2018-02-04
用的py3,使用的是pow函数,pow(x,y[,z])==x**y%z,一开始写的是pow(x,0.5,1)==0,出错,后来查到当参数z存在时,x,y必须为整数,且y不能为负。改为pow(x,0.5)%1==0。然后输出还要转换为list。
import math
def f(x):
return pow(x,0.5)%1==0
print(list(filter(f,range(1,101))))
import math
def f(x):
return pow(x,0.5)%1==0
print(list(filter(f,range(1,101))))
2018-02-04
def uppername(L):
for x in range(1, len(L)):
return L[0].upper() + L[x:].lower()
L = ['adam', 'LISA', 'barT']
print(map(uppername,L))
for x in range(1, len(L)):
return L[0].upper() + L[x:].lower()
L = ['adam', 'LISA', 'barT']
print(map(uppername,L))
2018-02-04
def add(x, y, f):
return f(x) + f(y)
def f(L):
return L ** 0.5
print(add(25, 9, f))
return f(x) + f(y)
def f(L):
return L ** 0.5
print(add(25, 9, f))
2018-02-04
print filter(lambda s:len(s.strip())>0 if s else False, ['test', None, '', 'str', ' ', 'END'])
emm 用if写的虽然结果对但是不能通过
emm 用if写的虽然结果对但是不能通过
2018-02-04
import time
def performance(f):
def fn(x):
print 'call', f.__name__, '()in', time.time()
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 'call', f.__name__, '()in', time.time()
return f(x)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
评论里有写代码好可怕。。。
2018-02-03
from math import *
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, sqrt)
2018-02-03
def format_name(s):
return s.lower().capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.lower().capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-02-02