我遇到了一个与实例属性有关的奇怪问题。我有一个变量logger,我希望它是一个实例属性。但是,我得到错误AttributeError: can't set attribute,除非将属性logger移到__init__()函数之外,这意味着我将logger声明为类属性(不是我想要的)。在
下面是我的代码片段:class MyScraper(ABC,scrapy.Spider):
"""Abstract base class for scraping non JS Web pages"""
#logger = None # Commented out, as I don't want class instance
def __init__(self, *args, **kwargs):
self.connection = None
self.channel = None
self.topic = None
log_format = "%(asctime)s - %(levelname)s - %(message)s"
log_level = 10
handler = TimedRotatingFileHandler("{0}.log".format(kwargs['log_filename']), when="midnight", interval=1)
handler.setLevel(log_level)
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
# add a suffix which you want
handler.suffix = "%Y%m%d"
#need to change the extMatch variable to match the suffix for it
handler.extMatch = re.compile(r"^\d{8}$")
self.logger = logging.getLogger('my_logger') #
# finally add handler to logger
self.logger.addHandler(handler)
# Set up messaging infrastructure ...
为什么我会遇到这个错误,以及如何修复它?在