已采纳回答 / 理Risen
(刚看到这个问题的时候我也感到很疑惑,下面的回答其实也是我的思考过程,通过测试来一步步获取知识点)首先要知道一些关于“伪私有属性”的概念:python的类中通过加双下划线来设置的“...
2018-08-08
#这个闭包和迭代器放一起,挺好的,受教了.
#返回的是一个函数组成的可迭代对象.
#厉害了.
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r = f(i)
fs.append(r)
return fs
f1, f2, f3 = count()
print (f1(), f2(), f3())
#返回的是一个函数组成的可迭代对象.
#厉害了.
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r = f(i)
fs.append(r)
return fs
f1, f2, f3 = count()
print (f1(), f2(), f3())
2018-08-07
#闭包
import functools
def chengji(x,y):
return x*y
def calc_prod(lst):
def inner():
return functools.reduce(chengji,lst)
return inner
f = calc_prod([1, 2, 3, 4])
print f()
import functools
def chengji(x,y):
return x*y
def calc_prod(lst):
def inner():
return functools.reduce(chengji,lst)
return inner
f = calc_prod([1, 2, 3, 4])
print f()
2018-08-07
def calc_prod(lst):
def calc():
def xy(x, y):
return x*y
return reduce(xy, lst)
return calc
f = calc_prod([1, 2, 3, 4])
print f()
def calc():
def xy(x, y):
return x*y
return reduce(xy, lst)
return calc
f = calc_prod([1, 2, 3, 4])
print f()
2018-08-07
def cmp_ignore_case(s1, s2):
if str.lower(s1) < str.lower(s2):
return -1
elif str.lower(s1) > str.lower(s2):
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if str.lower(s1) < str.lower(s2):
return -1
elif str.lower(s1) > str.lower(s2):
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-08-07
最新回答 / I_am_东风98K
if self.score==s.score://如果分数相同,执行下面代码 return cmp(self.name,s.name)//分数相等比较名字,进行排序return -cmp(self.score,s.score)//前面添加负号,用来锁定分数始终是从高排到低,
2018-08-07
最新回答 / 呆萌新
你说你调用upper(s[0])会报错,我想问你有没有这个upper(str a) 这个函数存在?没有当然报错啊?那什么函数存在?str.upper()这个是存在的
2018-08-07
def calc_prod(lst):
def prod():
sum=1
for i,v in enumerate(lst):
sum*=v
return sum
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
sum=1
for i,v in enumerate(lst):
sum*=v
return sum
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-08-06
最赞回答 / 光荣交白卷哥
回答一下你的问题。用vsc或者pycharm软件打开load函数定义信息,"""Deserialize ``fp`` (a ``.read()``-supporting file-like object containing a JSON document) to a Python object. """这一段属于描述,大概意思是通过解析fp类型内容,也就是类似于文件的对象的时候,会先执行read函数,也就是说load函数执行的时候,会率先执行read函数,而对象本身虽然不是文件类型,但是他和文件类型...
2018-08-06
import math
def is_sqr(x):
y=math.sqrt(x)
return y==int(y)
print filter(is_sqr, range(1, 101))
def is_sqr(x):
y=math.sqrt(x)
return y==int(y)
print filter(is_sqr, range(1, 101))
2018-08-05
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score >80:
return 'A-优秀'
elif self.__score >=60:
return 'B-及格'
else:
return 'C-不及格'
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score >80:
return 'A-优秀'
elif self.__score >=60:
return 'B-及格'
else:
return 'C-不及格'
2018-08-05
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 AttributeError:
print p.name
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 AttributeError:
print p.name
print 'attributeerror'
这个问题很简单,捕获异常信息,并且输出异常名称,可以通过编译器。
2018-08-05
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-08-05
class Person(object):
count = 0
def __init__(self,name):
self.name = name
Person.count = Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
count = 0
def __init__(self,name):
self.name = name
Person.count = Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2018-08-05