Bools and explicitness [was Re: PyWart: The problem with "print"]
Ned Batchelder
ned at nedbatchelder.com
Tue Jun 4 13:25:37 EDT 2013
On 6/4/2013 12:19 PM, Rick Johnson wrote:
> On Jun 4, 11:00 am, Chris Angelico <ros... at gmail.com> wrote:
>> You know, if you want a language with strict type declarations and
>> extreme run-time efficiency, there are some around.
> I don't like declaring types everywhere, i hate it. I prefer duck
> typed languages, HOWEVER, in order for duck typing to work
> consistently you must have checks and balances that the programmer can
> apply when he feels necessary. My "is_valid" built in will bridge the
> gap. We won't be forced to declare types, but we should ALWAYS add
> "type checks" to our "truth tests" unless we want to create subtle
> bugs. "is_valid" IS the answer!
>
You are mis-using the term "duck typing." It doesn't mean just, "no type
declarations." It also means, "the type of the value is irrelevant, all
that matters is what it can do." Insisting that something be a list (or
a dict, ...) is unnecessary and counter to the duck-typing philosophy.
What's important is that you can iterate, or index it, or whatever it is
you want to do with the list. The abstract base classes in the
collections module were designed to help with determining these
capabilities:
http://docs.python.org/2/library/collections.html#collections-abstract-base-classes
Of course, often, it's best just to do what you want to do rather than
checking first.
Also, I have no idea why [] isn't a "valid" list. Surely different
applications will have different needs for what counts as valid once the
type-check is passed.
--Ned.
More information about the Python-list
mailing list