<story><title>The Composition over Inheritance Principle (2020)</title><url>https://python-patterns.guide/gang-of-four/composition-over-inheritance/</url></story><parent_chain><item><author>ravenstine</author><text>Why even use a class in any of those cases? The if-statement example demonstrates precisely how OO classes can make code more complicated than it needs to be.&lt;p&gt;Just make a log() function that takes a target (file, socket, etc) and a message. Unless your logger needs to be so flexible as to work in 3rd party apps, why can&amp;#x27;t it be just that simple?&lt;p&gt;Although the author&amp;#x27;s take on composition is agreeable, it&amp;#x27;s things like logging where I think custom classes usually don&amp;#x27;t make things better in the first place. Just writing a function with conditional logic gets the job done and is plenty maintainable.</text></item></parent_chain><comment><author>goto11</author><text>This is a general problem with code examples. You want the example to simple enough to easily understand. But many features only really makes sense in larger programs, so in a minimal example they seem overly complicated.&lt;p&gt;You don&amp;#x27;t want the code which logs a message to be coupled to the logging mechanism. You should be able to transparently change the target (file&amp;#x2F;socket&amp;#x2F;database or a combination) without having to change code and dependencies every place a message is logged.&lt;p&gt;If you don&amp;#x27;t need any of that, you just use print().</text></comment>
<story><title>The Composition over Inheritance Principle (2020)</title><url>https://python-patterns.guide/gang-of-four/composition-over-inheritance/</url></story><parent_chain><item><author>ravenstine</author><text>Why even use a class in any of those cases? The if-statement example demonstrates precisely how OO classes can make code more complicated than it needs to be.&lt;p&gt;Just make a log() function that takes a target (file, socket, etc) and a message. Unless your logger needs to be so flexible as to work in 3rd party apps, why can&amp;#x27;t it be just that simple?&lt;p&gt;Although the author&amp;#x27;s take on composition is agreeable, it&amp;#x27;s things like logging where I think custom classes usually don&amp;#x27;t make things better in the first place. Just writing a function with conditional logic gets the job done and is plenty maintainable.</text></item></parent_chain><comment><author>sevensor</author><text>&amp;gt;  Just make a log() function that takes a target (file, socket, etc) and a message. Unless your logger needs to be so flexible as to work in 3rd party apps, why can&amp;#x27;t it be just that simple?&lt;p&gt;Of all the ways this could have been done, it seems like they chose the most complicated.&lt;p&gt;Python&amp;#x27;s lambdas get a lot of grief, but&lt;p&gt;&lt;pre&gt;&lt;code&gt;    def my_logger():
        log1 = lambda msg: log(sys.stderr, msg)
        log2 = lambda msg: log1(filter_function(msg))
        return lambda msg: log2(other_filter_function(msg))
&lt;/code&gt;&lt;/pre&gt;
seems like an entirely sane and uncomplicated way build up a logging function that does what you want it to.  Sure, it&amp;#x27;s not going to be high-performing code (these are Python lambdas after all), but it&amp;#x27;s not like the article&amp;#x27;s version is going to take home any prizes in that department either.</text></comment>