finish_endtag in sgmllib.py [Python 2.4]

R

Richard Hsu

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 == "": # ?

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) ??

thank you in advance.

Richard Hsu
hobbyist programmer.
Toronto, Canada.
 
B

Ben Cartwright

Richard said:
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
 
R

Richard Hsu

thank you Ben. not only did i learn something about my question, i
learnt the 'truth' :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top