oop - Python Class that Never Throws an AttributeError or TypeError -


what best way implement class never throws attributeerror or typeerror?

the idea have class this

class a():     def __init__(self):         self.logger = neverthrowclass()     def set_logger(self, logger):         self.logger = logger     def do_stuff(self)         self.logger.info('stuff being done.')         pass 

whenever class methods classed, write log file. if no log file specified, still want class methods function. realize catch exception whenever self.logger accessed, rather have default logger attribute magical neverthrowclass() accept being called in anyway, , politely nothing, , return none attribute value class still function fine whether logger specified or not.

something this, maybe?

class donothing(object):     def __getattr__(self, name):         return lambda *args, **kwargs: none 

basically, class responds attribute access function accepts number of arguments , returns none.

logger = donothing() logger.info("stuff being done")  # nothing 

since it's using __getattr__() rather lower-level __getattribute__(), can throw regular attributes on there if want non-method stuff.

class loggernot(donothing):     loglevel = 0 

now have loglevel attribute has reasonable value (i.e. not function) if have code checks it.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -