这里的可变参数kw为字典,使用索引代入特定参数即可
class Person(object):
def __init__(self, name, gender, **kw):
self.name=name
self.gender=gender
self.age=kw['age']
self.course=kw['course']
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
class Person(object):
def __init__(self, name, gender, **kw):
self.name=name
self.gender=gender
self.age=kw['age']
self.course=kw['course']
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2018-02-05
class BStudent(BasketballMixin,Student):
def __init__(self):
super(BStudent, self).skill()
class FTeacher(FootballMixin,Teacher):
def __init__(self):
super(FTeacher, self).skill()
def __init__(self):
super(BStudent, self).skill()
class FTeacher(FootballMixin,Teacher):
def __init__(self):
super(FTeacher, self).skill()
2018-02-05
def count():
fs = []
for i in range(1, 4):
def g(i):
def x():
return i * i
return x
fs.append(g(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def g(i):
def x():
return i * i
return x
fs.append(g(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-02-05
def f(s):
s=s.lower()
s=s[0].upper()+s[1:]
return s
print map(f, ['adam', 'LISA', 'barT'])
s=s.lower()
s=s[0].upper()+s[1:]
return s
print map(f, ['adam', 'LISA', 'barT'])
2018-02-05
前面一节不会看答案了可以理解毕竟没接触过time包,但是这一节有前面的代码为什么还有那么多人吐槽。真的很难吗?学东西动动脑子好吧!!
2018-02-05
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
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