refactoring - Abstract class with Python 2.5 -


i refactor class defining client or server. class had lot of

if client:     xxxxxxxxxxxx elif server:     yyyyyyyyyyyy 

so decided create class similar code , 1 class c client , other 1 s server inherit a. (they don't have theses names of course ^^)

so class kind of abstract class. problem there no abstract classes in python 2.5, comes 2.6 version. wondering if there way forbid instantiations of class a.

one solution have been raise notimplemented error in constructor of class a, c , s have same code put in "abstract" class (bad idea ?).

this may seem stupid develop in python time time , i'm young programmer. advices?

in statically-typed languages, use abstract base class (abc) because need object defined size, interface etc. pass around. otherwise, code trying call methods on object can't compiled.

python isn't statically-typed language, , calling code doesn't need know type of object it's calling @ all. so, can "define" abc documenting interface requirements, , implementing interface directly in 2 unrelated classes.

eg,

class server:     def do_thing(self):         pass #do server thing here  class client:     def do_thing(self):         pass #do client thing here  def do_thing(thingy):     thingy.do_thing() # client? server? else?  s=server() c=client()  do_thing(s) do_thing(c) 

here, pass in any object do_thing method arguments match call.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -