difference between variables inside and outside of __init__()

Is there any difference at all between these classes besides the name?

class WithClass ():
    def __init__(self):
        self.value = "Bob"
    def my_func(self):
        print(self.value)

class WithoutClass ():
    value = "Bob"

    def my_func(self):
        print(self.value)

Does it make any difference if I use or don’t use the __init__ method for declaring the variable value?

My main worry is that I’ll be using it one way, when that’ll cause me further problems down the road.

12 Answers
12

Leave a Comment