2017-10-15 46 views
0

的实例之间支持我必须安装在Windows 7版本的Python 3.6.2,我得到以下错误:“>”不“方法,包装”和“廉政”

TypeError: '>' not supported between instances of 'method-wrapper' and 'int'

哪有我解决这个问题?

# create a subclass and override the handler methods 
class MyHTMLParser(HTMLParser): 
    # function to handle an opening tag in the doc 
    # this will be called when the closing ">" of the tag is reached 
    def handle_starttag(self, tag, attrs): 
     pos = self.getpos() # returns a tuple indication line and character 
     print ("At line: ", pos[0], " position ", pos[1]) 
     if attrs. __len__ > 0: 
      print ("\tAttributes: ") 
      for a in attrs: 
       print ("\t", a[0], "=", a[1]) 

回答

2

所有__len__首先不是一个属性,它是一种方法,而你需要调用它是这样的:attrs.__len__()。但是,为什么在有非常好的方法时调用dunder方法 - len - 使用起来更简单,需要的字符更少?

if len(attrs): 
    ... 
+1

它工作。非常感谢,特别是对于更简单的方法:) – Sparky

相关问题