python - Inheritance and customisation of class -


i define 1 class device lot of subclasses example microcontroller.

device has 2 mode , "simulated" mode.

i trying code device class such has if microcontroller in simulated mode when print performed should prefix string [simulated]:

print "hey !!" > [simulated] hey!! 

i don't know how start , if it's possible overload print.

you can implement logger class, , use logging. can dome object injection replace type of logger when want. example:

class logger:     def __init__(self, prefix):         self.prefix = prefix     def log(self, msg):         print('[{}] {}'.format(self.prefix, msg))   class base:     def set_logger(self, logger):         self.logger = logger   class test(base):     def do_something(self):         self.logger.log('hey !!')   test = test()  test.set_logger(logger('simulated')) test.do_something()  test.set_logger(logger('very real')) test.do_something() 

for example:

[simulated] hey !! [very real] hey !! 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -