def count():
fs = []
for i in range(1, 4):
def f(m=i):
return m*m
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
个人感觉,原题都等于9,因为返回的是函数,参数是变量i,当f1,f2,f3都等于count()时,i的值变成3,后面再执行时,理解成指针,最后调用的时候i是指向3的(内存中有3个地址给i)。
改后的程序,也是返回函数,参数是m,(内存分别指向123)
fs = []
for i in range(1, 4):
def f(m=i):
return m*m
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
个人感觉,原题都等于9,因为返回的是函数,参数是变量i,当f1,f2,f3都等于count()时,i的值变成3,后面再执行时,理解成指针,最后调用的时候i是指向3的(内存中有3个地址给i)。
改后的程序,也是返回函数,参数是m,(内存分别指向123)
2016-04-02
import os
print (not os.path.isdir(r'/data/webroot/resource/python'))
print (not os.path.isfile(r'/data/webroot/resource/python/test.txt'))
因为文件不在。。。。
print (not os.path.isdir(r'/data/webroot/resource/python'))
print (not os.path.isfile(r'/data/webroot/resource/python/test.txt'))
因为文件不在。。。。
2016-04-01
class Person:
def __init__(self,name,gender,birth,**kw):
self.name=name
self.gender=gender
self.birth=birth
self.kw=kw
p=Person("xiaoming","male","1990-1-1",job="student")
print p.name
print p.kw
老师的代码优点麻烦,我觉得这个比较简单
def __init__(self,name,gender,birth,**kw):
self.name=name
self.gender=gender
self.birth=birth
self.kw=kw
p=Person("xiaoming","male","1990-1-1",job="student")
print p.name
print p.kw
老师的代码优点麻烦,我觉得这个比较简单
2016-04-01
>>> help(property)
| class C(object):
| @property
| def x(self):
| "I am the 'x' property."
| return self._x
| @x.setter
| def x(self, value):
| self._x = value
| @x.deleter
| def x(self):
| del self._x
就仅仅为了不写长的函数名......
| class C(object):
| @property
| def x(self):
| "I am the 'x' property."
| return self._x
| @x.setter
| def x(self, value):
| self._x = value
| @x.deleter
| def x(self):
| del self._x
就仅仅为了不写长的函数名......
2016-03-31
def __str__(self):
return '(Student: %s, %s,%s)' % (self.name, self.gender,self.score)
__repr__=__str__
s = Student('Bob', 'male', 88)
print s.__repr__()
return '(Student: %s, %s,%s)' % (self.name, self.gender,self.score)
__repr__=__str__
s = Student('Bob', 'male', 88)
print s.__repr__()
2016-03-31