[英]AttributeError: can't set attribute - Python instance attribute assignment
我遇到了一個與實例屬性相關的奇怪問題。 我有一個變量logger
,我想將其作為實例屬性。 但是,我收到錯誤AttributeError: can't set attribute
除非我將屬性logger
移到__init__()
函數之外,這(IIRC)意味着我將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') # <- barfs here
# finally add handler to logger
self.logger.addHandler(handler)
# Set up messaging infrastructure ...
為什么我會收到此錯誤,以及如何修復它?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.