[Zope] Python Script control flow question

Dieter Maurer dieter@handshake.de
Wed, 12 Sep 2001 00:33:12 +0200 (CEST)


H.de.Wit@SFK.NL writes:
 > This should be terribly simple, but i just don't see how it should be.
 > I have a python script, it handels the output of a SQL statement with a
 > SELECT COUNT in it.
 > It tests the case that the count is zero (and that there is no record in
 > the SQLresult).
 > 
 > This is my idea how it should be:
 > SQLresult=container.SQL_statement()
 > if (not SQLresult):
This part will never be entered. You should always get a single row.
 >    resultcount=0
 > else:
 >    resultcount=SQLres[0][0]
 > return resultcount
 > 
 > This doesn't return a value if the resultcount is 0, nothing seem to happen
 > during testing the python script.
 > It works fine if the resultcount>0
Apparently, a caller of your method does something like:

	    if result: do something
	    else: do nothing

As "0" is a Python false value, nothing happens.

 > This however is a working script
 > SQLresult=container.SQL_statement()
 > if (not SQLresult):
 >    resultcount=0
 >    print resultcount
 >    return printed
 > else:
 >    resultcount=SQLres[0][0]
 >    return resultcount
The "print" converted the "0" into the string "'0'" which is not
a Python false value.

 > It is ugly, but it works. Is it possible to remove the print resultatcount
 > and the return printed somehow?
You can use:

    return `resultcount`

This, too, converts the integer into a string.


Dieter