class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count += 1
print(Person.__count)
p1 = Person('Bob')
p2 = Person('Alice')
try:
print(Person.__count)
except AttributeError:
print("AttributeError")
__count = 0
def __init__(self, name):
self.name = name
Person.__count += 1
print(Person.__count)
p1 = Person('Bob')
p2 = Person('Alice')
try:
print(Person.__count)
except AttributeError:
print("AttributeError")
2018-02-06
import math
def is_sqr(x):
if math.sqrt(x) - int(math.sqrt(x)) == 0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x) - int(math.sqrt(x)) == 0:
return x
print filter(is_sqr, range(1, 101))
2018-02-05
def count():
fs = []
for i in range(1, 4):
def f(i):
def fn():
return i*i
return fn
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i):
def fn():
return i*i
return fn
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-02-05
这里的可变参数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