I'm trying to make a class based on an instance of another class. For this I use the __new__
method:
import logging
class Logger:
def __new__(cls):
logger = logging.getLogger('main')
# make a queue in a thread to put log messages in a PyQt text browser console
return logger
def close(self):
pass
# close the thread
def main():
logger = Logger()
# more things
logger.close()
if __name__ == '__main__':
main()
I am getting an AttributeError:
AttributeError: 'Logger' object has no attribute 'close'
My idea was to make a class that wraps around the instance returned from logging.getLogger('main') and be able to call both its original methods (like setLevel) and add my own.
The use of the logging module is not vital for my question, but it is an example of me not knowing how to use subclassing in this case.
My questions are:
- What is going wrong? How could I make this work while using the
__new__
method? - I've been wanting to do this more often. Is this a stupid thing to do in the first place? What would be a better way?