← Back to team overview

p2psp team mailing list archive

Inheritance and subclass creation

 

Hi everybody,

when implementing the first part of NTS, I sometimes got confused how
inheritance is implemented in P2PSP, especially the instance creations
in peer.py and splitter.py. It seems strange to me at first that (e.g.
when creating a Peer_DBS from a Peer_IMS) a new object is created that
inherits the methods from the base class but copies the attributes from
the base class instance.
However I like the idea that you create a base object and extend its
functionality after finding out what is needed. Just out of curiosity,
is this a common design pattern in Python?
As an alternative method to achieve the same effect, you could
dynamically set the extending/inheriting class (to avoid copying the
necessary attributes and thus simplifying the classes' constructors)
like this (similar to [1], some discussion is in [2]):

    # peer = Peer_DBS(peer) would look like this:
    peer.__class__ = Peer_DBS

What do you think about this? Some do even say "Never do this in
production." [3]
Or would it be imaginable to automatically "inherit" the base class
attributes by copying the attributes dict to the new instance?
Just some random thoughts I got when implementing the NTS classes
inheriting DBS...

Thanks,
Max

[1]
http://stackoverflow.com/questions/8544983/dynamically-mixin-a-base-class-to-an-instance-in-python/8545134#8545134
[2]
http://stackoverflow.com/questions/13280680/how-dangerous-is-setting-self-class-to-something-else#13280789
[3]
http://stackoverflow.com/questions/4838145/assigning-to-an-instances-class-attribute-in-python#4838197

Follow ups