question about True values

Antoon Pardon apardon at forel.vub.ac.be
Fri Oct 27 05:39:56 EDT 2006


On 2006-10-26, Donn Cave <donn at u.washington.edu> wrote:
> In article <mailman.1264.1161861732.11739.python-list at python.org>,
>  Steve Holden <steve at holdenweb.com> wrote:
> ...
>> 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.
>
> It is idiomatic, but not _just_ idiomatic.  The former requires
> a list object (or a tricky __eq__()), the latter works with a variety
> of objects, exhibiting a useful polymorphism.

The latter will treat None and False the same as [], () and {},
which in most of my code is not what I want. In most cases
I find testing for an empty sequence (however you do it)
useless, because the loop over the sequence does whatever
I want if it is empty. In cases where I do want to test
for it I usually write:

  if len(my_list) > 0:

That provides the polymorphism that is usefull to me and
doesn't treat None the same as an empty sequence.

> As for similarities between computer programming languages
> and natural languages, I think that breaks down pretty fast.
>
> Part of the problem is something that pinches Python pretty
> hard right here, a lack of words that conveniently express
> important concepts in the language.  A few posts back, Carl
> Banks made a distinction between "equaling" and "being", and
> if I understood that right, it expresses a fundamental notion
> about the meaning of Python's "if", "while" etc. statements.
> Unfortunately, though, English conflates existence and identity
> in this word ("be"), so it's not going to serve our purposes
> very well, and when it comes to words like "if" -- well, we
> just have to use what we have.
>
> If there were better words to use with the notion of
> "something-ness", I think we would see booleans as a silly
> thing of little use to Python programmers.

I think you are incorrect. Decisions have to be made and
they are made based on conditions. Conditions are expressed
in terms of True and False not in terms of Nothing or Something.

That is how IMO people think. You can't change that just
because python is implemented with the Nothing vs Something
distinction in mind.

> If you can see
> "if" and "while" as constructs that respond to something-ness,
> you will appreciate idiomatic Python better, because that
> arguably is just what it's about.

And how do I express that a number has to be greater than
100 into a Nothing vs Something dichotomy? Declare all
greater numbers as Something and the rest as Nothing?

-- 
Antoon Pardon



More information about the Python-list mailing list