CIL icon indicating copy to clipboard operation
CIL copied to clipboard

remove `hasattr`s

Open casperdcl opened this issue 1 year ago • 1 comments

vis TomographicImaging/CIL?q=hasattr

much bug such bad:

class Foo:
    def run(self):
        if not hasattr(self, 'bar'):
            self.bar = 0
        self.bar += 1
    def stop(self):
        self.bar -= 1  # might raise AttributeError

correct:

class Foo:
    def __init__(self):
        self.bar = 0
    def run(self):
        self.bar += 1
    def stop(self):
        self.bar -= 1

casperdcl avatar Feb 07 '24 15:02 casperdcl

With optional attributes:

class Foo:
    def __init__(self):
        self.bar = 0
        self.baz = None

    def run(self):
        self.bar = 0

        if self.baz is not None:
            self.baz += 1

WYVERN2742 avatar Feb 07 '24 16:02 WYVERN2742