Interfaces and Protocols in Python
glyph.twistedmatrix.com
glyph.twistedmatrix.com
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...
Can't interfaces do that too? You can just say builtins.set implements the receiver interface.
But I don't think the "you can declare a protocol after several types already implement it" is any different under explicit vs implicit conformance checking.
I can just say
implementer(IReceiver)(builtins.set)
at any time. There's no temporal restriction on when I can do that.Or add a single type annotations to the method declaring that the parameter satisfies a protocol.
implementer(IReceiver)(builtins.set)
only needs to be written once, not once at each call site. It's telling the global registry that builtins.set implements IReceiver.In Java, you have to explicitly mark a class as conforming to an interface inside the class's declaration. You need to have the source and modify it.
In Haskell, you mark a type as conforming to an interface (called typeclass) by adding functions that operate on its instances and implement the interface's requirements. You can do it anywhere, and write an implementation for an existing type, which is normal practice.
In Python, you cannot easily add interface conformance, but possibly you can make a trivial subclass of the desired class, and mark it as conforming to the desired interface (Protocol), without modifying the source.
Not sure that says more about Zope or my bootstrapping skills, which are admittedly pretty poor.
It had some cool things, that I started missing once I moved on. How well the permission system, workflow engine and hierarchical content structure worked together, undo functionality, accessing things via urls like “/path/to/object/method” instead uf unreadable params, component based UI, having a single configuration file (zc.buildout) to have a reproducible environment for development and production, etc.
In many regards they were ahead of the time I think, at least in the space of things between simple script driven websites and very enterprisey solutions.
- (number_of_people_who_enjoy_coding_with_python_more_than_coding_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?)
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.
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.
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?
Yes, they can contain implementation logic. They don't have to though.