I am fairly new to python, and noticed these posts: Python __init__ and self what do they do? and Python Classes without using def __init__(self)
After playing around with it, however, I noticed that these two classes give apparently equivalent results-
class A(object):
def __init__(self):
self.x = 'Hello'
def method_a(self, foo):
print self.x + ' ' + foo
(from this question)
and
class B(object):
x = 'Hello'
def method_b(self,foo):
print self.x + ' ' + foo
Is there any real difference between these two? Or, more generally, does __init__
change anything inherently about the attributes of a class? In the documentation it is mentioned that __init__
is called when the instance is created. Does this mean that x
in class B
is established before instantiation?