super() in injected methods

Andras Tantos python-list at andras.tantosonline.com
Fri Feb 12 13:15:40 EST 2021


On 2/11/21 10:35 PM, Greg Ewing wrote:
> On 12/02/21 3:39 pm, Andras Tantos wrote:
>> Now, when a Port gets assigned a NetType, it needs to gain all sorts 
>> of new features. It for example should have a 'length' attribute that 
>> tells how many bits are needed to represent its possible values.
>
> The way I would probably approach this is to have a single
> Port type with all the methods that might be required, and
> forward their implementations to the NetType.
>
> e.g.
>
> class Port:
>
>     @property
>     def length(self):
>         return self.net_type.length
>
> Another possibility might be to change the __class__ of the port
> object at run time to a subclass of Port having the required
> features. That would be a lot easier and more efficient than
> adding individual methods to every Port instance, and super()
> should work normally.
>
That's actually a pretty good idea, thanks! Let me turn it around in my 
head to see how to integrate it into the library.

Thanks again,
Andras




More information about the Python-list mailing list