finish_endtag in sgmllib.py [Python 2.4]

Ben Cartwright bencvt at gmail.com
Tue Apr 11 21:13:02 EDT 2006


Richard Hsu wrote:
> code:-
>
>    # Internal -- finish processing of end tag
>     def finish_endtag(self, tag):
>         if not tag:  # <---- i am confused about this
>             found = len(self.stack) - 1
>             if found < 0:
>                 self.unknown_endtag(tag)  # <---- and this
>                 return
>
> I am a little confused as to what is intended by " if not tag: "
> does it mean
> if tag == None or tag == "":  # ?

Technically, not quite.  See http://docs.python.org/lib/truth.html

In practice, tag will indeed be a string type (shouldn't ever be None),
so 'tag == ""' would work just as well* as 'not tag'.  However, it's
cleaner and clearer to use the latter.

* = barring some contrived custom string type

> tag here is suppose to be a string.
>
> so the only way it will be True is when its either None or its "", then
> we are essentially passing None or "" to self.unknown_endtag(tag) ??

Yes, a string of length zero will always be passed to unknown_endtag
here.  Answering your implicit question, there's no good reason to
write it as "self.unknown_endtag(None)" instead.

--Ben




More information about the Python-list mailing list