[HN Gopher] Interfaces and Protocols in Python
___________________________________________________________________
Interfaces and Protocols in Python
Author : BerislavLopac
Score : 112 points
Date : 2021-03-17 08:52 UTC (2 days ago)
(HTM) web link (glyph.twistedmatrix.com)
(TXT) w3m dump (glyph.twistedmatrix.com)
| decafninja wrote:
| Have some bad memories with zope.interface. Worked on a team
| composed mainly of ex enterprise .NET developers forced to work
| with Python (myself included). Some embraced the Python paradigm,
| others stubbornly clinged to old enterprise .NET habits.
| zope.interface was heavily used by the latter to essentially
| write something as close as possible to an enterprise .NET
| application, only in Python.
| macintux wrote:
| I tried to create a web service using Zope in 2002 or so. I
| struggled for weeks trying to figure out how to get anything
| done, never did produce anything useful.
|
| Not sure that says more about Zope or my bootstrapping skills,
| which are admittedly pretty poor.
| dagw wrote:
| Yea, Zope was weird. I learnt python in around 2000 and
| immediately fell in love with the language. Then I needed to
| to do some web stuff and wanted to move beyond simple cgi
| scripts and for some reason decided to pick up Zope. Almost
| stopped using Python all together then and there.
| padobson wrote:
| This is what I was thinking about reading this post. Why add
| things to Python to make it strongly typed? Why not just use
| Java or C#?
| Gibbon1 wrote:
| Because Java and especially C# aren't 'cool'
| 6gvONxR4sf7o wrote:
| The libraries that make my job easier are in python. A
| language ecosystem is often more important than the language
| itself.
| ralmidani wrote:
| Not an exhaustive list of possible reasons:
|
| - (number_of_people_who_enjoy_coding_with_python_more_than_co
| ding_with_c_sharp_or_java > 0) == True
|
| - Some people have large existing codebases, and would rather
| add types gradually than have to port all their logic to
| another language.
|
| - In terms of being free and open source, Python > C# > Java
| (meta: can you even do a comparison like that in C# and/or
| Java?)
| nerdponx wrote:
| I thought Java being not-open-source in 2021 was a myth,
| no? What about OpenJDK (GPL licensed)?
| ralmidani wrote:
| GPL2 has no explicit patent grant, and Oracle's behavior
| does not signal good intentions.
| dragonwriter wrote:
| > Why add things to Python to make it strongly typed?
|
| Python is strongly typed. The support for external tools is
| to enable static type checking, not strong typing.
|
| > Why not just use Java or C#?
|
| Why add "dynamic" to C#, why not just use Python?
| nerdponx wrote:
| 1. Because people like Python.
|
| 2. The type system is both _gradual_ and not enforced by
| default. This means you benefit from the rapid prototyping
| and general freedom of dynamically-typed code, but you have
| the option to layer on the additional safety of static type
| checking as the project grows. There aren 't many other
| gradually-typed languages out there anyway; Julia, Typed
| Racket, and Common Lisp aren't exactly in widespread industry
| use for general-purpose server dev.
| smallnamespace wrote:
| Don't forget JavaScript/TypeScript.
|
| You can either start with JS and add types later on (you
| can mix them on a per-module/file basis), or just start
| with TypeScript, with a loose configuration.
| carlosf wrote:
| I've recently found about Protocol / structural subtyping in
| Python and found this sort of pattern really useful as duck
| typing is so common in py code.
|
| Highly recommend reading:
|
| https://www.python.org/dev/peps/pep-0544/
|
| https://docs.python.org/3/library/typing.html#nominal-vs-str...
|
| https://docs.python.org/3/library/typing.html#typing.Protoco...
|
| Cool example:
|
| https://github.com/jordaneremieff/mangum/blob/main/mangum/ty...
| neolog wrote:
| > Unlike Interfaces, Protocols can describe the types of things
| that already exist.
|
| Can't interfaces do that too? You can just say builtins.set
| implements the receiver interface.
| joshuamorton wrote:
| You have to say that builtins.set implements the interface. If
| there's a receiver protocol, it'll just accept a set of that
| matches the protocol, no registration required.
| zeugmasyllepsis wrote:
| Not without modifying the existing type. Since Interfaces rely
| on nominal typing, each type implementing an interface must
| explicitly declare that they do so. On the other hand, since
| Protocols are checked structurally you can declare a protocol
| _after_ several types already implement it, and type checking
| will pass without having to modify the implementing types.
___________________________________________________________________
(page generated 2021-03-19 23:02 UTC)