在中间加上一行f=f()让函数执行就可以了
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
f=f()
fs.append(f)
return fs
f1, f2, f3 = count()
print f1, f2, f3
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
f=f()
fs.append(f)
return fs
f1, f2, f3 = count()
print f1, f2, f3
2019-03-30
# 通过[._Person__score]调用该属性
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
try:
print p._Person__score
except AttributeError:
print('AttributeError')
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
try:
print p._Person__score
except AttributeError:
print('AttributeError')
2019-03-25
import functools
sorted_ignore_case = functools.partial(sorted ,key = str.lower,reverse = False)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted ,key = str.lower,reverse = False)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2019-03-25
def count():
fs = []
for i in range(1, 4):
def mul(i):
return i*i
fs.append(count())
return fs
print (fs)
fs = []
for i in range(1, 4):
def mul(i):
return i*i
fs.append(count())
return fs
print (fs)
2019-03-25
for i in range(1, 4):
def f(): # 因为f函数没有传递参数,所以函数f的定义里面i就是未知量。
# i=1时,得到函数f1():i*i i值在函数外
print(i*i) # i=2时,得到函数f2():i*i
# i=3时,得到函数f3():i*i
fs.append(f)
f() # 如果定义后马上调用执行,因为有i的值,就得到当前i对应的函数值。
def f(): # 因为f函数没有传递参数,所以函数f的定义里面i就是未知量。
# i=1时,得到函数f1():i*i i值在函数外
print(i*i) # i=2时,得到函数f2():i*i
# i=3时,得到函数f3():i*i
fs.append(f)
f() # 如果定义后马上调用执行,因为有i的值,就得到当前i对应的函数值。
2019-03-16
import functools
def sort(data,lower):
if lower==True:
data=map(lambda x:x.lower(),data)
return sorted(data)
sorted_ignore_case = functools.partial(sort,lower=True)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
def sort(data,lower):
if lower==True:
data=map(lambda x:x.lower(),data)
return sorted(data)
sorted_ignore_case = functools.partial(sort,lower=True)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2019-03-13
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'
2019-03-13
def __cmp__(self, s):
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
elif self.score == s.score:
return cmp(self.name, s.name)
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
elif self.score == s.score:
return cmp(self.name, s.name)
2019-03-12
import math
def is_sqr(x):
return x==int(math.sqrt(x))**2
print filter(is_sqr, range(1,101))
def is_sqr(x):
return x==int(math.sqrt(x))**2
print filter(is_sqr, range(1,101))
2019-03-07