More On the Strange Problem

MRAB python at mrabarnett.plus.com
Wed Dec 23 16:00:27 EST 2009


Victor Subervi wrote:
> On Wed, Dec 23, 2009 at 3:03 PM, MRAB <python at mrabarnett.plus.com 
> <mailto:python at mrabarnett.plus.com>> wrote:
> 
>     Victor Subervi wrote:
> 
>         I've isolated the problem in the print-out at the bottom of this
>         post. It occurs when these values are passed:
> 
>         ['LastDatePrice', 'date', '10', 'yyyy/mm/dd', None],
> 
>         Since this is the first datetime that is passed, it would appear
>         to be associated with such values, which are processed thus:
> 
>            elif typeName == 'datetime':
> 
>     Where does the value of typeName come from?
> 
> It is the descriptor of the MySQL type.
> 
>              print "<input type='text' width='%s' maxlength='%s'
>         name='%s' value='%s' /> " \
>                "<i>This field takes a datetime up to %s alphanumeric
>         characters inclusive.</i>" \
>                % (shortNum, str(typeNum), colName, theVal, str(typeNum))
> 
>         colName == 'LastDatePrice'
>         typeNum = 10
> 
>              elif 10 < int(typeNum) < 20:
> 
>     If typeNum is 10 then 10 < int(typeNum) < 20 is False.
> 
> Thank you for catching that! However, the code is followed by an else 
> clause.
> 
>                shortNum = '10'
> 
>         Therefore...
>         shortNum = '10'
> 
>            if whatDo == 'insert':
>              theVal = defaultVal
>              val = defaultVal
> 
>     What's the difference between theVal and val? The names don't help me!
>     :-)
> 
>     if whatDo == 'insert':
>       theVal = defaultVal
>       val = defaultVal
>     else:
>       theVal = val
>  
>         Since whatDo == 'insert'...
>         and
>         val = 'yyyy/mm/dd'
>         therefore
>         theVal = None
> 
[snip]
> 
>         If in fact the problem has to do with the None value, how can I
>         easily substitute a different value? I tried:
> 
>         if theVal == None:
> 
>     The Pythonic way is to use "is" when comparing with singletons like
> 
>     None.
> 
> 
> Can you please explain by example?
>  
Singleton: there is only ever one of certain objects and using "is" lets
you check for identity. For example, consider identical twins; they look
the same, but are not the same person. In programming terms you could
imagine the following scenario:

 >>> print first_twin == second_twin
True
 >>> print first_twin is second_twin
False

In Python by far the most common singleton is None; you should use "is"
(or "is not") when checking whether (or not) an object is actually None.
There might also be a few cases where a certain implementation of Python
might use singletons for reasons of efficiency, but they are just
optimisations in that implementation and shouldn't be relied on. The
rule is: if you're not checking for identity (and, as I said, by far the 
most common use is with None), check for equality.

> 
>          theVal = ''
> 
>         but that didn't capture it.
> 
>     What do you mean by "didn't capture it"?
> 
> The output was identical to what I originally posted.

I recommend that you write messages and values to a log file along the
path in the code you think it's following and then see whether it's
doing what you think it is and with the values you think it should have.



More information about the Python-list mailing list