[issue37857] Setting logger.level directly has no effect due to caching in 3.7+

Vinay Sajip report at bugs.python.org
Mon Sep 9 06:02:05 EDT 2019


Vinay Sajip <vinay_sajip at yahoo.co.uk> added the comment:

That code in the wild that sets the level attribute directly is wrong and should be changed, right? The documentation has always been clear that setLevel() should be used. If we now take steps to support setting the level via attribute, isn't that encouraging bypassing the documented APIs? I'm not sure such misuse is widespread.

Using a property might seem a good solution, but there is a performance impact, so I am not keen on this change. I don't know what the real-world performance impact would be, but this simple script to look at the base access timing:

import timeit

class Logger(object):
    def __init__(self):
        self._levelprop = self.level = 0

    @property
    def levelprop(self):
        return self.level


def main():
    logger = Logger()
    print(timeit.timeit('logger.level', globals=locals()))
    print(timeit.timeit('logger.levelprop', globals=locals()))

if __name__ == '__main__':
    main()

gives, for example,

0.12630631799993353
0.4208384449998448

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37857>
_______________________________________


More information about the Python-bugs-list mailing list