←back to thread

249 points mattcollins | 1 comments | | HN request time: 0.209s | source
Show context
gigatexal ◴[] No.42190772[source]
I’m gonna buy the book but I prefer composition over OOP. I prefer to have an init that takes some params where those params are fully baked clients of whatever services I need and then the class just uses them as needed. I don’t see a lot of value in having a Python class that might have a few or more classes that it extends where all the functions from all the classes crowd up the classes namespace.

Class Foo.__init__(self, db, blob_storage, secrets_manager, …)

Instead of class Foo(DB, BlobStorer, SecretsMgr)

Etc

replies(4): >>42190800 #>>42190856 #>>42191086 #>>42193088 #
1. vram22 ◴[] No.42193088[source]
>I’m gonna buy the book but I prefer composition over OOP.

The GoF book (the design patterns book) says in a page right near the start, "Prefer composition over inheritance", in the middle of an otherwise blank page, presumably to emphasize the importance of that advice.

As others have replied, composition is one technique you can use in OOP, not something that is the opposite of OOP.

You can also use composition in non-OOP procedural languages like C, by having a struct within a struct.

https://www.google.com/search?q=can+you+have+nested+structs+...