type(XXX) == instance
2017-09-06 本文已影响0人
小颖啊
1.instance从何而来
在python2中,如果定义类时继承了object,那么实例化后对象的type就是该类
class test(object):pass
typeTest = test();
print type(typeTest)
但如果定义类的时候没有继承object,实例化后对象的type将会是 instance
class test(): pass
typeTest = test();
print type(typeTest) == instance
举个栗子吧:
def getContent(self, pageNum):
url = url = self.baseUrl+ self.seeLZ + '&pn=' + str(pageNum)
request = urllib2.Request(url)
response = urllib2.urlopen(request, None, 100000)
**********************
return response ------------------------1
return response.read().decode('utf-8')--------------------------2
*************************************
def findTitile(self):
page = self.getContent(1)
pattern = re.compile('<h1 class="core_title_txt.*?>(.*?)</h1>',re.S)
resut = re.search(pattern, page)-------------------------3
if resut:
return resut.group(1).strip()
else:
print "none"
return None
findTitile()
注:
3的结果取决于getContent()的返回值,当返回值为1时,返回是instance类型;当返回值为2时,返回类型是unicode(String)。热,search(pattern,(string or buffer)) 所以当返回值为1时会报错,错误截图如下: