在中間加上一行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