def get_header(self, name, default=None):
value = self.request.headers.get(name, default)
if not value:
return value
return to_unicode(value, errors="replace")
But I don't like repeating value so much
def get_header(self, name, default=None):
if not (value := self.request.headers.get(name, default)):
return value
return to_unicode(value, errors="replace")
The parenthesis and walrus operator make the line too convoluted
Fixes https://github.com/scrapy/scrapy/issues/6308
Alternatives:
But I don't like repeating value so much
The parenthesis and walrus operator make the line too convoluted
Using try/except this way is pythonic, but I'm always afraid I may unexpectedly capture another unrelated exception