def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-01-04
def __init__(self, num):
self.num = num
self.d = self.x()
def __len__(self):
return len(self.d)
self.num = num
self.d = self.x()
def __len__(self):
return len(self.d)
2018-01-04
def a(L):
for s in L:
if isinstance(s,Student):
pass
else:
return 'Bug'
b = a(L)
if b == 'Bug':
print b
else:
print sorted(L)
for s in L:
if isinstance(s,Student):
pass
else:
return 'Bug'
b = a(L)
if b == 'Bug':
print b
else:
print sorted(L)
2018-01-04
print '<Student: name, gender, score>'
2018-01-04
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
setattr(self,'dd',kw)
for k,v in getattr(self,'dd').items():
setattr(self,k,v)
self.name = name
self.gender = gender
setattr(self,'dd',kw)
for k,v in getattr(self,'dd').items():
setattr(self,k,v)
2018-01-04
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count +1
def put_count(self):
print Person.__count
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count +1
def put_count(self):
print Person.__count
2018-01-04
p1 = Person('Bob')
p1.put_count()
p2 = Person('Alice')
p2.put_count()
try :
print Person.__count
except AttributeError:
print 'attributeerror'
p1.put_count()
p2 = Person('Alice')
p2.put_count()
try :
print Person.__count
except AttributeError:
print 'attributeerror'
2018-01-04
import math
def is_sqr(x):
y=math.sqrt(x)
if y.is_integer():
return y
print filter(is_sqr, range(1,101))
def is_sqr(x):
y=math.sqrt(x)
if y.is_integer():
return y
print filter(is_sqr, range(1,101))
2018-01-04
class Fib(object):
def __init__(self, num):
i=0
n=0
self.list=[]
while(i<num):
if i==1:
n=1
self.list.append(n)
if len(self.list)>1:
n=n+self.list[-2]
i=i+1
def __init__(self, num):
i=0
n=0
self.list=[]
while(i<num):
if i==1:
n=1
self.list.append(n)
if len(self.list)>1:
n=n+self.list[-2]
i=i+1
2018-01-03
最赞回答 / 慕盖茨9455599
这样做其实是正确的,但是这样你就没法利用父类中的逻辑了,可能在父类的初始化函数中进行了一些逻辑判断,当然你也可以完全拷贝下来,这样就失去了继承的意义了
2018-01-03