from functools import partial
sorted_ignore = partial(sorted, key=str.lower)
print(sorted_ignore(['bob', 'about', 'Zoo', 'Credit']))
sorted_ignore = partial(sorted, key=str.lower)
print(sorted_ignore(['bob', 'about', 'Zoo', 'Credit']))
2016-01-18
已采纳回答 / 火野剑去
个人理解是这样的:如果grade不是私有属性,即外部就可以访问(既可设值也可取值),这种情况下,@property 根本就没有意义了. 因为@property的产生就是为了不让你在类外部直接访问类内部的属性.
2016-01-17
from functools import reduce
def calc_prod(lst):
def lazy_prod():
return reduce(lambda x, y: x*y, lst)
return lazy_prod
f = cacl_prod([1, 2, 3, 4])
print(f())
def calc_prod(lst):
def lazy_prod():
return reduce(lambda x, y: x*y, lst)
return lazy_prod
f = cacl_prod([1, 2, 3, 4])
print(f())
2016-01-17
python3用法:
a = ['bob', 'about', 'Zoo', 'Credit']
print(sorted(a, key=str.lower))
a = ['bob', 'about', 'Zoo', 'Credit']
print(sorted(a, key=str.lower))
2016-01-17
import math
def is_sqr(x):
return str(math.sqrt(x)).split('.')[-1] == '0'
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return str(math.sqrt(x)).split('.')[-1] == '0'
print filter(is_sqr, range(1, 101))
2016-01-17
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 AttributeError:
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 AttributeError:
print ('attributeError')
2016-01-16
def __str__(self):
x = min(self.p, self.q)
while self.p % x != 0 or self.q % x != 0:
x -= 1
if x == 1:
break
return "%s/%s" % (self.p/x, self.q/x)
x = min(self.p, self.q)
while self.p % x != 0 or self.q % x != 0:
x -= 1
if x == 1:
break
return "%s/%s" % (self.p/x, self.q/x)
2016-01-16
最新回答 / 夏天的微笑_
print 'call %s() in %fs' %(f._name_,(t1-t2))中的f._name_不对,应该是f.__name__,name前后是两个下划线;还有应该是t2-t1。
2016-01-16
def calc_prod(lst):
def mul():
if not isinstance(lst, list):
# raise Exception()
print 'arg is not a list'
return
else:
return reduce(lamda: x,y: x*y, lst)
return mul
f = calc_prod([1, 2, 3, 4])
print f()
这段代码哪里有问题,请问?
def mul():
if not isinstance(lst, list):
# raise Exception()
print 'arg is not a list'
return
else:
return reduce(lamda: x,y: x*y, lst)
return mul
f = calc_prod([1, 2, 3, 4])
print f()
这段代码哪里有问题,请问?
2016-01-16
import json
class Students(object):
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
class Students(object):
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
2016-01-16
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
try:
print p.name
print p.__score
except:
print "attributeerror"
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
try:
print p.name
print p.__score
except:
print "attributeerror"
2016-01-15