class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print "attributeerror"
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print "attributeerror"
2016-01-08
def format_name(s):
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-01-07
已采纳回答 / 努力提升
区别很大。s.upper()意思是调用s的upper()函数;math.sqrt(x)意思是调用math的sqrt()函数,而x是参数,x通过sqrt()函数的计算然后返回相应的数值。
2016-01-07
def cmp_ignore_case(s1, s2):
if s1.upper() < s2.upper():
return -1
if s1.upper() > s2.upper():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper() < s2.upper():
return -1
if s1.upper() > s2.upper():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-01-07
import math
def comp(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(comp,range(1,101))
def comp(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(comp,range(1,101))
2016-01-07
带参数的decorator函数首先是返回一个不带参数的decorator函数,接着把函数f传入decorator函数中进行装饰,再返回装饰后的函数。
2016-01-07
def calc_prod(l):
def calc():
def f(x,y):
return x*y
return reduce(f,l)
return calc
f=calc_prod([1,2,3,4])
print f()
def calc():
def f(x,y):
return x*y
return reduce(f,l)
return calc
f=calc_prod([1,2,3,4])
print f()
2016-01-07
def f(x1,x2):
u1=x1.upper()
u2=x2.upper()
if u1>u2:
return 1
if u1<u2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'],f)
u1=x1.upper()
u2=x2.upper()
if u1>u2:
return 1
if u1<u2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'],f)
2016-01-07
加上第三个参数:
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12],1)
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12],1)
2016-01-07
def f(x):
return x[0:1].upper()+x[1:].lower()
print map(f,['adam','LISA','barT'])
return x[0:1].upper()+x[1:].lower()
print map(f,['adam','LISA','barT'])
2016-01-07
最新回答 / lc云泽
print 调用的是__str__方法,应该从__str__函数中找返回值;而return len()是__len__方法的返回值,所以需要在外部调用__len__才可以
2016-01-06