This may be a stupid / trivial question, but I'm confused in this matter.
What is the encouraged (pythonic) way of declaring instance fields - in the constructor, or in the class body itself?
class Foo:
""" Foo class """
# While we are at it, how to properly document the fields?
bar = None
def __init__(self, baz):
""" Make a Foo """
self.bar = baz
OR:
class Foo:
""" Foo class """
def __init__(self, baz):
""" Make a Foo """
self.bar = baz
"""bar initial value"""below the field.barin__init__()and inadvertently end up with a class attribute (and a hard-to-find bug). For these reasons, I'd use #2 every day of the week.self.foo = 123, does that override the "class attribute" effect and make it an instance attribute? Because if so, then I'm fine with this - the class attribute will just hold a never-changed default value.