Bools and explicitness [was Re: PyWart: The problem with "print"]

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jun 5 01:28:49 EDT 2013


On Wed, 05 Jun 2013 02:27:26 +1000, Chris Angelico wrote:

> On Wed, Jun 5, 2013 at 2:19 AM, Rick Johnson
> <rantingrickjohnson at gmail.com> 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!
> 
> Option 1:
> 
> void C_function(int x)
> 
> Option 2:
> 
> def Python_function(x):
>     assert isinstance(x,int)
> 
> Is there a fundamental difference? You're basically proposing Option 2
> while detesting Option 1.


How many years has Rick been coming here, proclaiming loudly how much he 
loves Python's duck-typing? Ten years? And yet, he still has no clue what 
it actually means.

If you're performing a type-check, IT ISN'T DUCK-TYPING.

Duck typing means you don't care whether you have an int, so long as 
whatever object you get is usable where an int is usable. 

Now, there are many places in my own code where I decide that I wish to 
prohibit duck-typing. "Here I insist on an actual int, not just any old 
number." There are, sometimes, good reasons for this. But every time I do 
this, I am *not* duck-typing, I am doing a runtime type check which is 
completely opposite in intent to duck-typing. (There's no short name for 
this -- it's not quite static typing, because it happens at runtime and 
isn't enforced by the language.) 

It's almost like Rick declares that he's a great supporter of the free 
market, and that everyone should be free to buy and trade in whatever 
property they wish, while insisting that nothing can be bought or sold 
without permission from the government first.



-- 
Steven



More information about the Python-list mailing list