super() in injected methods

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Feb 12 01:35:36 EST 2021


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.

-- 
Greg


More information about the Python-list mailing list