class Person(object):
__count = 0
def __init__(self, name):
Person.__count += 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'AttributeError'
__count = 0
def __init__(self, name):
Person.__count += 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'AttributeError'
2018-04-03
class Person(object):
__count = 0
def __init__(self, name):
Person.__count += 1
self.name = name
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'AttributeError'
__count = 0
def __init__(self, name):
Person.__count += 1
self.name = name
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'AttributeError'
2018-04-03
python中/表示浮点数,//表示整除,另外还有导入from __future__ import division,要不会报错
2018-04-03
已采纳回答 / qq_龙_18
这个不能那样计算的,你可以单独试试看 print(isinstance(math.sqrt(4),int)),这个的结果也是错的,因为math.sqrt()出来的是flout 所以不是int
2018-04-03
def calc_prod(lst):
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-04-02
def format_name(s):
return s.capitalize()
# return s.title()
# return s[0].upper()+s[1:].lower()
# return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
# return s.title()
# return s[0].upper()+s[1:].lower()
# return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-04-02
class Fib(object):
def __init__(self, num):
self.num=num
def __str__(self):
L=[0,1]
for x in range(0,self.num-2):
L.append(L[-1]+L[-2])
return str(L)
def __len__(self):
return self.num
f = Fib(10)
print f
print len(f)
def __init__(self, num):
self.num=num
def __str__(self):
L=[0,1]
for x in range(0,self.num-2):
L.append(L[-1]+L[-2])
return str(L)
def __len__(self):
return self.num
f = Fib(10)
print f
print len(f)
2018-04-02
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-04-02
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
2018-04-02
最赞回答 / du_mooc
kw.iteritems()是generator, yield 2-tuple。从传入参数(字典)**kw中取k,v。setattr(self,k,v)xiang当于self.k = v 语句。**kw 是传入字典参数,例 a=1, name='bob'...
2018-04-02
最新回答 / 小阳阳7758321
def f1(x): return x*2def new_fn(f): def fn(x): print 'call '+f.__name__+'()' return f1(x) return fnf1=new_fn(f1)print f1()写成这样会无限循环调用装饰器
2018-04-02
为什么老报这个错误:TypeError: '<' not supported between instances of 'Student' and 'Student'
2018-04-02
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:
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:
print 'AttributeError'
2018-04-02