Talks - Hynek Schlawack: Subclassing, Composition, Python, and You
HTML-код
- Опубликовано: 28 авг 2024
- Ever seen a code base where understanding a simple method meant jumping through tangled class hierarchies? We all have! And while "Favor composition over inheritance!" is almost as old as object-oriented programming, strictly avoiding all types of subclassing leads to verbose, un-Pythonic code. So, what to do?
The discussion on composition vs. inheritance is so frustrating because far-reaching design decisions like this can only be made with the ecosystem in mind - and because there's more than one type of subclassing!
Let's take a dogma-free stroll through the types of subclassing through a Pythonic lens and untangle some patterns and trade-offs together. By the end, you'll be more confident in deciding when subclassing will make your code more Pythonic and when composition will improve its clarity.
For me, this has been one of the best talks of pycon 2023 so far. I’m wondering where can I get the slides.
Excellent talk! I will certainly rewatch this, and I will recommend it to any Python developer I talk to.
The sound level is way too low. Otherwise it’s an excellent talk.
In my opinion Repository is an anti-pattern while developing in Python, it's great in other languages but it's not so good in python. The sole reason for it is asyncio module.
If you're planning to use repositories and allow usage of Async APIs, your domain logic must be async to (at least functions which are meant to access repositories)
My solution was to never use repositories inside domain layer.
That's not correct, for example:
async def my_service_layer_func(request):
my_domain_object = await some_repo(request.resource_id)
my_domain_object.some_completely_synchromous_domain_method()
await some_repo.save(my_domain_object)
I've been using the repo pattern for abt 2.5 years in prod, in an async codebase, and my domain layer is completely synchronous. True, if you want to inject repos into your domain layer your domain will have to be async, but that's an antipattern anyway. So that's essentially your code telling you you're trying to implement a bad idea.
@@wesselbindt If you read the book mentioned in this Talk (which is "Cosmic python") you'll see that authors suggest to use repositories in domain service layer. Various articles on DDD in python suggest the same, so it's pretty much considered a "pattern", not an "anti-pattern".
It is positioned as a way do decouple IO from our domain layer, so services in domain-layer are not bothered by the concrete implementations of storages, and whole point of repos is to inject them into domain layer. Which couples our domain layer on concrete IO implementation, which is bad.
Personally I use repositories as a way to construct business objects in my application layer and pass them to my domain layer, which decouples domain layer from the chosen IO model.