Another Sets Problem

Steve Holden steve at holdenweb.com
Mon Dec 28 16:23:34 EST 2009


Victor Subervi wrote:
> On Mon, Dec 28, 2009 at 3:07 PM, Steve Holden <steve at holdenweb.com
[...]
>     >          However, when I try that in my current script, the script
>     >         fails. It throws no error, but rather just quits printing
>     to the
>     >         screen. Here's the code snippet:
>     >
>     >                      elif types[x][0:3] == 'set':
>     >                        for f in field:
>     >                          print '<td>%s</td>\n' % (field)
>     >                      else:
>     >                        print '<td>%s</td>\n' % (field)
>     >
>     >     [snip]
>     >
>     >     You're printing the entire field for each value in the field.
>     Is this
>     >     intentional?
>     >
>     >
>     > It doesn't matter. The code ceases to execute with the line:
>     >
>     > for f in field:
>     > beno
>     >
>     Well it looks to me like types[x][0:3] == 'set' and f is empty. That
>     wouldn't produce any printed output.
> 
> 
> It's not empty. I've printed it. But if it were, it would look like this:
> set([])
> and so this would be left to print:
> ([])
> beno
> 
There is only one way for the piece of code you quote to print nothing
(unless you suspect a bug in the Python interpreter, but the probability
of that is so low compared with the probability of your making a mistake
in interpretation of the data that I am going to ignore it).

Hence my deduction. If types[x][0:3] != 'set' then the else clause would
definitely print something. Hence types[x][0:3] == 'set', and the for
statement is executing. But again, if field were anything other than an
empty container it would produce printed output.

But if it's empty, no printed output would be produced:

>>> field = set([])
>>> for f in field:
...   print "Steve is wrong"
...
>>>

Does this make sense?

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list