def __
self.fib=[]
if num==0:
self.fib=[]
elif num==1:
self.fib=[0]
elif num==2:
self.fib=[0,1]
else:
self.fib=[0,1]
for i in range(2,num):
self.fib.append(self.fib[i-2]+self.fib[i-1])
self.fib=[]
if num==0:
self.fib=[]
elif num==1:
self.fib=[0]
elif num==2:
self.fib=[0,1]
else:
self.fib=[0,1]
for i in range(2,num):
self.fib.append(self.fib[i-2]+self.fib[i-1])
2019-08-21
已采纳回答 / 天很蓝zpf
因为当你把g函数去掉,只保留return j*j 后,f 函数返回的就是一个int,那么fs这个集合就是int集合,那么count()方法返回的就是int集合,所以此时f1、f2、f3都是int,无法print f1(),就会报这个错: 'int' object is not callable
2019-08-21
感觉这种写法是值得注意和思考的:
from os import path
print path.isdir(r'/data/webroot/resource/python')
print path.isfile(r'/data/webroot/resource/python/test.txt')
from os import path
print path.isdir(r'/data/webroot/resource/python')
print path.isfile(r'/data/webroot/resource/python/test.txt')
2019-08-21
已采纳回答 / 天很蓝zpf
此处把s用作逻辑“与”运算,与运算两边的表达式都是布尔类型,True或者False,在python里面如果s是整数0,空值None,空字符串'',空集合( )都会被判定为False。单单一个len(s.strip())>0只能用于判断是否空字符串'',但0、空值none、空集合( )都用不了s.strip(),所以,将s放在and前面可以先过滤,当s是整数0,空值None,空字符串'',空集合( )时判定为False,那么and运算这时就“短路”直接返回False,当s是' '这种字符串时,s...
2019-08-21
def __cmp__(self, s):
if self.score<s.score:
return 1
elif self.score>s.score:
return -1
else:
if self.name<s.name:
return -1
elif self.name>s.name:
return 1
else:
ret
if self.score<s.score:
return 1
elif self.score>s.score:
return -1
else:
if self.name<s.name:
return -1
elif self.name>s.name:
return 1
else:
ret
2019-08-20
def __str__(self):
return '(Student: %s,%s,%s)' %(self.name, self.gender, self.score)
return '(Student: %s,%s,%s)' %(self.name, self.gender, self.score)
2019-08-20
lass Person(object):
def __init__(self, name, gender, **kw):
self.name =name
self.gender=gender
for k,v in kw.iteritems():
setattr(self,k,v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name =name
self.gender=gender
for k,v in kw.iteritems():
setattr(self,k,v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2019-08-20
def read(self):
return '["Tim","Bob","Alice"]'
增加read方法
return '["Tim","Bob","Alice"]'
增加read方法
2019-08-20
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course=course
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course=course
2019-08-20
class Person(object):
count=0
def __init__(self,name):
self.name=name
Person.count+=1
count=0
def __init__(self,name):
self.name=name
Person.count+=1
2019-08-20
try:
print p.__score
except AttributeError:
print ("AttributeError")
print p.__score
except AttributeError:
print ("AttributeError")
2019-08-20