2017-10-15 11 views
0

J'ai la version Python 3.6.2 installé sur Windows 7 et je reçois l'erreur suivante:'>' pas pris en charge entre les instances de 'méthode wrapper' et 'int'

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

Comment puis-je répare ça?

# 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]) 

Répondre

2

d'abord __len__ n'est pas un attribut, il est une méthode, et vous aurez besoin de l'appeler comme ceci: attrs.__len__(). Mais pourquoi appeler les méthodes dunder quand il y a une méthode parfaitement bonne - len - qui est plus simple à utiliser et nécessite moins de caractères?

if len(attrs): 
    ... 
+1

Cela a fonctionné. Merci beaucoup, surtout pour la méthode plus simple :) – Sparky