Implicit conversion to boolean in if and while statements

Chris Angelico rosuav at gmail.com
Sun Feb 10 05:50:25 CET 2013


On Sun, Feb 10, 2013 at 3:26 PM, Rick Johnson
<rantingrickjohnson at gmail.com> wrote:
> On Friday, February 8, 2013 11:01:00 PM UTC-6, Chris Angelico wrote:
>> [...]
>> Another advantage of using two characters: There's no conflict between
>> set and dict literals. How do you notate an empty set in Python? {}
>> means an empty dict.
>
> What makes you believe that a language must provide literal syntax for EACH and EVERY type? And BTW, if you don't already know, this is how you notate an empty set in Python:
>
> py> set([])
> set([])

Or omit the argument, to avoid working with a pointless empty list.
But what happens if you first execute:

set = tuple

? This is not a set literal, it's an expression that usually returns a set.

> IMO "Set Types" should only exists as a concequence of "freezing" an array, and should have NO literal syntax avaiable.

I don't understand. Wouldn't freezing an array (list) result in a
tuple? And, why should there be no literal syntax for them?

Having a convenient literal notation for every basic type is extremely
handy. You can work with integers 1, 2, 3, or floats 1.0, 2.0, 3.0. C
gives you those. In Python, you can work with lists, too, and C
doesn't give you those (you have array *initializer* syntax, but
that's not the same thing). It's perfectly plausible to dereference a
literal:

dow = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][day%7]

Of course, it's perfectly plausible to do that with a function, too.
You could define something like this:

def agg(*args): return args
dow = agg("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")[day%7]

But the question is, why? Why call a function when the interpreter can
do the work directly at compile stage? There's absolutely no value in
forcing things to be done at run-time.

ChrisA



More information about the Python-list mailing list