Defining instance variables in __init__ vs class's namespace?

40 views Asked by At

This had been unclear for me since I began Python: Why would I use this __init__ version:

class FooInst:
    def __init__(self, x=None):
        self.x = x

Over this version which uses class variables:

class FooCls:
    x = None

The second example obviously leads into much less code that seems easier to read to me. Also, there's little to no difference in everyday use:

inst = FooInst()
cls = FooCls()
print(inst.x, cls.x)  # None, None

inst.x = 1
cls.x = 1
print(inst.x, cls.x)  # 1, 1

So basically I they both have a default value of None and there's no difference between getting the value (obj.x in both cases). I can also set both variables to a value, and it'll work (since cls.x = 1 "replaces" the class variable's value by creating an instance variable).

I know that __init__ is used to create instance variables and class Foo: x = None creates class variables, but why? I find the way of creating class variables to work just fine with instance variables too, and it's much cleaner code!

EDIT: I would use __init__ though if the variable needs some custom behaviour, like this:

class Foo:
    def __init__(self, x=0):
        try:
            self.x = int(x)
        except ValueError as e:
            log_error(e)
            raise e

EDIT2: This question is more about immutable default values, and why should they be done in the __init__ (if they should?) rather than in the class's namespace, so it's not a duplicate to the marked question.

EDIT3: Also, I'm more like asking if there's any reason not to use class variables instead of instance variables; answers like "class variables are shared between all instances, instance variables aren't" don't answer my question, since (as shown below in my code examples) class variables work just as well.

0

There are 0 answers