an oop question

Julieta Shem jshem at yaxenu.org
Thu Nov 3 14:48:41 EDT 2022


ram at zedat.fu-berlin.de (Stefan Ram) writes:

> Julieta Shem <jshem at yaxenu.org> writes:

[...]

>   2. a. 1.  Remark
>
>   One can observe this ease especially when one defines a new
>   class with a standard verb and then standard procedures
>   "magically" use this new method, as in:
>
> class MyNewClass:
>     def __str__( self ):
>         return "Howdy!"
>
> print( MyNewClass() )
>
>   How can "print" possibly know about the method "__str__"
>   I just defined if "print" was written long before I defined
>   my class? <-- A beginner could ask this in bewilderment!

That's a good question.

>   2. b.  Adding a New Verb (Procedure) in Object-Oriented Programming
>
>   In object-oriented programming adding a new verb (a new
>   "procedure") is hard. Assume that now we would like to add
>   another verb such as "emit", say "length". All classes would
>   have to be changed and a new method definition for "length"
>   would have to be added to them! Some classes might even be
>   standard classes from libraries we can't easily change. 
>   So this clearly violates the Open-Closed-Principle!
>
>   3.  Comments
>
>   So, this would suggest to use procedural programming when
>   one foresees the need to add more object-specific procedures
>   later and object-oriented programming when one foresees the
>   need to add more types later.
>
>   The problems with OOP which make adding new verbs violate
>   the open-closed principle possibly would not occur in 
>   a language where one could add new methods to a library
>   class in a user program.

Thank you!  That did clarify everything for me!  (I'll find a way to
read Martin's book!)


More information about the Python-list mailing list