question about True values

Steve Holden steve at holdenweb.com
Thu Oct 26 07:22:50 EDT 2006


John Coleman wrote:
> Donn Cave wrote:
> 
>>In article <1161812827.596356.316450 at f16g2000cwb.googlegroups.com>,
>> "John Coleman" <jcoleman at franciscan.edu> wrote:
>>
>>
>>>Very good point, though one could argue perhaps that when one is
>>>comparing an object with a truth value then one is implicitly asking
>>>for the truth value of that object
>>
>>On the contrary -- since there is normally no need to ever
>>compare an object with a truth value, then I would interpret
>>this usage as an attempt to distinguish True or False from
>>other objects in general.  Some kind of placeholders for
>>missing values, who knows.  I'm not saying it's a great idea,
>>but it could work in recent versions of Python.
>>
>>
>>>This would make code like 'if s: ' equivalent
>>>to 'if s == True:' with a possible gain in readability. But - as you
>>>demonstrate the cost of that (minimal) gain in readability would be too
>>>high. In any event - I think it is mostly bad style to use a
>>>non-boolean variable in 'if s:' - it reminds me too much of obscure C
>>>code (though others might disagree).
>>
>>Others will indeed disagree.  I don't think you'll find
>>much support for this position.  But it's not as bad as
>>your notion that "if s == True", where s is not a boolean
>>object, might represent a gain in readability.  That really
>>redefines readability.
>>
>>   Donn Cave, donn at u.washington.edu
> 
> 
> As far as readability goes - most computer languages have a surface
> syntax which is (by design) vaguely similiar to the syntax of a natural
> language like English. Thus statements roughly correspond to sentences.
> For example, you can see a subject-verb-object pattern in "x=3". Given
> a genuinely boolean construct like x < 2, "if x < 2:" *sounds* right to
> a human reader. Similarly, given a good boolean variable with a
> descriptive name like "found", the fragment "if found:" sounds ok. But
> - given a non-boolean variable like "product_name" -  writing "if
> product_name:" sounds (to my ears) a little jarring - it sounds like a
> sentence fragment. It's like saying "If Bob" - If Bob *what*? Goes to
> the store? answers the telephone? Finish the damn thought! Now, if you
> were to (from some strange reason) use "product_name" as if it were a
> truth value then (to my ears) it would both make your intentions more
> clear and sound less fragmentary if you could say "if product_name ==
> True:". When you use "product_name" as the condition of an if statement
> then in that context it is *functioning* as a truth value so (naively)
> what could be wrong with comparing it to a truth value? I don't
> advocate any change in Python here - just pointing out that the idea of
> allowing "if s:" and "if s == True:" to be always equivalent in the
> interest of readability isn't *that* strange. It doesn't constitute a
> redefinition of readability.
> 
Yes, but you're talking about *your* ears there. I was pointing out, as 
others have, that "if product_name" is such a deeply-ingrained Python 
idiom that you can use how natural it "sounds" to you as a measure of 
your progress in the language.

> As far as using non-booleans as conditions - I just think that if you
> want a certain block of code to be executed only if, for example, a
> list is non-empty, why not *say* so? I think "if my_list != []:" just
> reads better than "if my_list:". I would think that my preferences
> there mesh with "Explicit is better than implicit" but apparently not.
> 
Maybe so, but that "rule" (and let's not forget that the zen is not 
actually a set of prescriptive rules but rather guidelines for the 
informed) is immediately preceded by the most important "rule" of all: 
"Beautiful is better than ugly". Nobody will shout at you (well, 
hopefully, not on this list they won't) for writing

   if my_list != []:
     ...

in your code, but if they have to incorporate it into their own they 
will almost certainly reduce it to

   if my_list:
     ....

It's just idiomatic in Python, the same way that "'Sup?" is idiomatic in 
English (or what passes for it nowadays ;-) but grates on those who 
aren't used to hearing it.

> I'm just starting out with Python with most of my programming in recent
> years being in various dialects of Visual Basic (which probably
> explains a lot). What attracts me to Python so far is the cool slice
> operations, the iterators, the libraries and the convience of
> programming in a REPL environment. So far, the ability to use "cat" as
> a part-time substitute for True just strikes me as a curiousity - but
> maybe that will change with time.
> 
It probably will, but I wouldn't get too hung up on what's definitely a 
small point. Enjoy Python the way it is, and the way you are. You and 
Python will definitely come to an accommodation (and you will love the 
combination of discipline and freedom that it brings to programming 
style). Welcome to the language!

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list