Re: Keeping Python a Duck Typed Language.
On Wed, Apr 21, 2021 at 4:44 PM Christopher Barker <pythonchb@gmail.com> wrote:
You say this like it's a bad thing, but how is this avoidable, even in principle? Structural typing lets you check whether Foo is duck-shaped -- has appropriate attribute names, etc. But quacking like a duck is harder: you also have to implement the Sequence behavioral contract, and realistically the only way to know that is if the author of Foo tells you.
But that's not what duck typing is (at least to me :-) ) For a given function, I need the passed in object to quack (and yes, I need that quack to sound like a duck) -- but I usually don't care whether that object waddles like a duck.
I agree with Christopher here. For duck typing, all we care is that the method(s) we need are implemented in a sensible way.
And this is not uncommon -- I suspect it's very rare for a single function to use most of the methods of a given ABC (or protocol, or whatever).
Also agree, and very often the function cares only about one single method.
And a lot of the standard library works exactly this way.
Totally. I found it astonishing that PEP 484 did not allow to correctly annotate LOTS of functions in the standard library, including built-ins people use every day like `sorted()`. Fortunately, that was corrected with PEP 544.
But there is the "culture" of Python -- and it has been very much shifting toward more typing
I disagree because Python is too widespread these days to talk about "the culture". We'll just need to learn to coexist as groups that use the language differently. -- A recent publication (sorry can't find it now -- my google fu is failing me) examined code on PyPi and found a lot of type hints -- many of which were apparently not being used with a static type checker. So why were they there? Maybe you mean this one: "Python 3 types in the wild: a tale of two type systems" Metadata: https://dl.acm.org/doi/10.1145/3426422.3426981 PDF: http://hirzels.com/martin/papers/dls20-python-types.pdf
And I've seen a lot more isinstance(Some_ABC) code lately as well.
I believe a lot of that is people coming from statically typed languages and not feeling comfortable with duck typing, while for some other reason not adopting type hints.
From looking at the work of my beginning students, I can tell that they are seeing examples out there that use more typing, to the point that they think it's a best practice (or maybe even required?).
It is considered best practice in some places. I hope we as a community never adopt it as the canonical way.
Maybe it is -- but if the community is moving that way, we should be honest about it.
Clearly parts of the community are moving that way. The paper I linked shows the use of type hints is still very limited in their sample.
I'm not even sure that this *is* nominal typing. You could just as well argue that "the operation `isinstance(..., Sequence)` returns `True`" is just another of the behavioral constraints that are required to quack like a sequence.
It is not just nominal typing because of the subclass hook.
I'm not sure of the definition of "nominal" typing -- but it absolutely is NOT duck typing (As Luciano pointed out, Alex Martelli coined the term Goose Typing for this).
Yes. I wish Alex joined this conversation. Cheers, Luciano On Wed, Apr 21, 2021 at 8:28 PM Luciano Ramalho <luciano@ramalho.org> wrote:
On Wed, Apr 21, 2021 at 4:44 PM Christopher Barker <pythonchb@gmail.com> wrote:
You say this like it's a bad thing, but how is this avoidable, even in principle? Structural typing lets you check whether Foo is duck-shaped -- has appropriate attribute names, etc. But quacking like a duck is harder: you also have to implement the Sequence behavioral contract, and realistically the only way to know that is if the author of Foo tells you.
But that's not what duck typing is (at least to me :-) ) For a given function, I need the passed in object to quack (and yes, I need that quack to sound like a duck) -- but I usually don't care whether that object waddles like a duck.
I agree with Christopher here. For duck typing, all we care is that the method(s) we need are implemented in a sensible way.
And this is not uncommon -- I suspect it's very rare for a single function to use most of the methods of a given ABC (or protocol, or whatever).
Also agree, and very often the function cares only about one single method.
And a lot of the standard library works exactly this way.
Totally. I found it astonishing that PEP 484 did not allow to correctly annotate LOTS of functions in the standard library, including built-ins people use every day like `sorted()`. Fortunately, that was corrected with PEP 544.
But there is the "culture" of Python -- and it has been very much shifting toward more typing
I disagree because Python is too widespread these days to talk about "the culture". We'll just need to learn to coexist as groups that use the language differently.
-- A recent publication (sorry can't find it now -- my google fu is failing me) examined code on PyPi and found a lot of type hints -- many of which were apparently not being used with a static type checker. So why were they there?
Maybe you mean this one:
"Python 3 types in the wild: a tale of two type systems" Metadata: https://dl.acm.org/doi/10.1145/3426422.3426981 PDF: http://hirzels.com/martin/papers/dls20-python-types.pdf
And I've seen a lot more isinstance(Some_ABC) code lately as well.
I believe a lot of that is people coming from statically typed languages and not feeling comfortable with duck typing, while for some other reason not adopting type hints.
From looking at the work of my beginning students, I can tell that they are seeing examples out there that use more typing, to the point that they think it's a best practice (or maybe even required?).
It is considered best practice in some places. I hope we as a community never adopt it as the canonical way.
Maybe it is -- but if the community is moving that way, we should be honest about it.
Clearly parts of the community are moving that way. The paper I linked shows the use of type hints is still very limited in their sample.
I'm not even sure that this *is* nominal typing. You could just as well argue that "the operation `isinstance(..., Sequence)` returns `True`" is just another of the behavioral constraints that are required to quack like a sequence.
It is not just nominal typing because of the subclass hook.
I'm not sure of the definition of "nominal" typing -- but it absolutely is NOT duck typing (As Luciano pointed out, Alex Martelli coined the term Goose Typing for this).
Yes. I wish Alex joined this conversation.
Cheers,
Luciano
-- Luciano Ramalho | Author of Fluent Python (O'Reilly, 2015) | http://shop.oreilly.com/product/0636920032519.do | Technical Principal at ThoughtWorks | Twitter: @ramalhoorg
-- Luciano Ramalho | Author of Fluent Python (O'Reilly, 2015) | http://shop.oreilly.com/product/0636920032519.do | Technical Principal at ThoughtWorks | Twitter: @ramalhoorg
participants (1)
-
Luciano Ramalho