on implementing a toy oop-system

Meredith Montgomery mmontgomery at levado.to
Mon Sep 5 13:07:12 EDT 2022


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

> Meredith Montgomery <mmontgomery at levado.to> writes:
>>Is that at all possible somehow?  Alternatively, how would you do your
>>toy oop-system?
>
>   Maybe something along those lines:
>
> from functools import partial
>
> def counter_create( object ):
>     object[ "n" ]= 0
> def counter_increment( object ):
>     object[ "n" ]+= 1
> def counter_value( object ):
>     return object[ "n" ]
>
> counter_class =( counter_create, counter_increment, counter_value )
>
> def inherit_from( class_, target ):
>     class_[ 0 ]( target )
>     for method in class_[ 1: ]:
>         target[ method.__name__ ]= partial( method, target )
>
> car = dict()
>
> inherit_from( counter_class, car )
>
> print( car[ "counter_value" ]() )
> car[ "counter_increment" ]()
> print( car[ "counter_value" ]() )
>
>   . The "create" part is simplified. I just wanted to show how
>   to make methods like "counter_increment" act on the object
>   that inherited them using "partial".

That's simple and interesting.  I'll study your more elaborate approach
next, but I like what I see already.  Thank you so much.


More information about the Python-list mailing list