0

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
Patrik Lippojoki
  • 1,603
  • 4
  • 19
  • 22
  • @MartijnPieters I've edited the question, it's not a duplicate now :) My code mostly contains `None` default values, and my question would be if there's any reason not to use class variables. – Patrik Lippojoki Jun 24 '15 at 09:54
  • @MartijnPieters Also see the edit #3, there's no answer to my question in the topic you linked. – Patrik Lippojoki Jun 24 '15 at 09:56
  • Well, class variables are class variables. `class Foo: bar = 'baz'` allows you to do `Foo.bar`. Does that make sense, is that reasonable behaviour? Arguably not. Does it make any difference **for you**? You decide. – deceze Jun 24 '15 at 10:04
  • @deceze That's the exact question I'm asking here. I know I can access the variable through the class (and changing the value through a class will change all the instances' default values too), **but**: if I were to use instance variables, I wouldn't be accessing the class variable *anyways* (because there wouldn't be one). So why not just use class variables if I'm not going to use it anyways and it looks twice cleaner in the code? – Patrik Lippojoki Jun 24 '15 at 10:12
  • It is *just a design choice* when they are immutable. There isn't much else to say here. – Martijn Pieters Jun 24 '15 at 10:17
  • Not sure what you mean by *"if I were to use instance variables, I wouldn't be accessing the class variable anyways (because there wouldn't be one)"*... But let's put it this way: **write what you mean**. The most compact source code is not a competition that's very fruitful to engage in. Write the code that expresses as straight forwardly as possible what the code is supposed to do. If one option is more concise than another *but it has a bunch of caveats and ifs and buts and as-long-as's*, then it's probably not the code you should be writing. – deceze Jun 24 '15 at 10:17

0 Answers0