-
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typeclasses in Python #1143
Comments
Typeclasses in PythonTypeclasses in PythonJune 30, 2021 24 mins read Start a discussion Edit this page Today I am going to introduce a new concept for Python developers: typeclasses. It is a concept behind our new I will tell you in advance, that it will look very familiar to what you already know and possibly even use. Moreover, we reuse a lot of existing code from Python’s standard library. So, you can call this approach “native” and “pythonic”. And it is still going to be interesting: I am showing examples in 4 different languages! But, before discussing typeclasses themselves, let’s discuss what problem they do solve. Some functions must behave differentlyOk, this one is a familiar problem to all of the devs out there. How can we write a function that will behave differently for different types? Let’s create an example. We want to
Note, that The first approach that comes to our minds is to use Here’s how it would look like:
The main limitation is that we cannot extend this function for other type easily (we can use wrapper function, but I consiser this a redefinition). But, in some cases - And that’s the part where things begin to get interesting. All programming languages address this problem differently. Let’s start with Python’s traditional OOP approach. OOP extendability and over-abstraction problemsSo, how does Python solve this problem? We all know that Python has magic methods for some builtin functions like Let’s say we want to greet a user:
You can use this method directly or you can create a helper with
And then we can use it:
So, it works? Not really. There are several problems. First, some classes do not want to know some details about themselves to maintain abstraction integrity. For example:
Does this Moreover, I think that mixing structure and behavior into a single abstraction is bad. Why? Because you cannot tell in advance what behavior you would need from a given structure. For abstractions on this level, it is way easier to have behavior near the structure, not inside it. Mixing these two only makes sense when we work on a higher level like services or processes. Second, it only works for custom types. Existing types are hard to extend. For example, how would you add the You can create
But, this would require a change in our usage:
Monkey-patchingSome might suggest that we can just insert the needed methods directly into an object / type. Some dynamically typed languages went on this path:
It is quite obvious, that it is not going to work for anything complex. Why?
I hope that it is clear: we won’t fall into this trap. Let’s consider another alternative. Extra abstractionsPeople familiar with things like
And we can use it like so:
But, now we have a different problem: we have a gap between real types and their wrappers. There’s no easy way to wrap a type into its wrapper. How can we match them? We have to do it either by hand or use some kind of registry like And it is still not enough, there will be runtime errors! In practice, it ends up like Typeclasses and similar conceptsLet’s look at how functional languages (and Some common knowledge:
ElixirLet’s start with one of my favorites.
I am pretty sure that my readers were able to read and understand Usage of the code above:
The thing with Protocols are very widely used, they power lots of the language’s features. Here are some real-life examples:
Rust
And of course, due to
See? The idea is so similar, that it uses almost the same syntax as Notable real-life examples of how
Basically, Haskell
So, what’s a typeclass? Typeclass is a group of types, all of which satisfy some common contract. It is also a form of ad-hoc polymorphism that is mostly used for overloading. I am a bit sorry for the
Basically, we do the same thing as we have already done for
Then we can use our new
Some real-life examples of typeclasses:
I would say that among our three examples, It is important to note that typeclasses from But, what about Python? dry-python/classesThere’s an awesome function in the Python standard library called It does exactly what we need. Do you still remember that we are finding a way to change the function’s behavior based on the input type? Let’s have a look!
Looks cool, moreover, it is in standard lib, you even don’t have to install anything! And we can use it like a normal function:
So, what’s the point in writing a completely different library like we did with We even reuse some parts of Better typingWith For example, you can pass unsupported types:
In
Or you can break the
But, not with
Which is, again, possible with
And you cannot restrict Protocols are unsupportedProtocols are an important part of Python. Sadly, they are not supported by
Protocols support is also solved with
No way to annotate typesLet’s say you want to write a function and annotate one of its arguments that it must support the
It is impossible with
ConclusionWe have come a long way, from basic stacked I have shown, that this native and pythonic idea deserves wider recognition and usage. And our extra features in As a result of using typeclasses, you will untangle your structures from behavior, which will allow you to get rid of useless and complex abstractions and write dead-simple typesafe code. You will have your behavior near the structures, not inside them. This will also solve the extendability problem of OOP. Combine it with other Future workWhat do we plan for the future? There are several key aspects to improve:
Stay tuned! If you like this article you can:
Subscribe to my blog if you want morePlease enable JavaScript to view the comments powered by Disqus. |
Today I am going to introduce a new concept for Python developers: typeclasses. It is a concept behind our new dry-python library called classes. I will tell you in advance, that it will look very familiar to what you already know and possibly even use.
Tags: python
via Pocket https://ift.tt/3h2N4Z4 original site
July 01, 2021 at 10:58AM
The text was updated successfully, but these errors were encountered: