← Back to team overview

p2psp team mailing list archive

Re: Inheritance and subclass creation

 

Hi Max!

Sorry, I can't remember why the inheritance was implemented in that way. I
think that your idea is simpler than the solution that now is used in the
current implementation. Please, go ahead :-)

Regards,
Vi.

On Sun, Jun 21, 2015 at 10:30 PM Max Mertens <max.mail@xxxxxxxxxx> wrote:

>  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
>
-- 
-- 
Vicente González Ruiz
Depto de Informática
Escuela Técnica Superior de Ingeniería
Universidad de Almería

Carretera Sacramento S/N
04120, La Cañada de San Urbano
Almería, España

e-mail: vruiz@xxxxxx
http://www.ual.es/~vruiz
tel: +34 950 015711
fax: +34 950 015486

Follow ups

References