oop - Inheritance vs class instance in python module import -


apologies if doesn't make sense, i'm not of experienced programmer.

consider following code:

import mymodule  class myclass:     def __init__(self):         self.classinstance = mymodule.classinstance() 

and ......

from mymodule import classinstance  class myclass(classinstance):     def __init__(self):         pass 

if wanted use 1 classinstance in myclass, ok import specific class module , have myclass inherit class ?

are there best practices, or things should thinking when deciding between these 2 methods ?

many thanks

allow me propose different example.

imagine have class vector. want class point. point can defined vector maybe has other functionalities vector doesn't have. in case derive point vector.

now need line class. line not specialisation of of above classes don't want derive of them. line uses points. in case might want start line class way:

class line(object):     def __init__(self):         self.point1 = point()         self.point2 = point() 

where point this:

class point(vector):     def __init__(self):         vector.__init__(self) 

so answer really: depends need do, when have clear idea of coding, choosing between sub-classing or not becomes obvious.

i hope helped.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -