Possible Duplicate:
Variables inside and outside of a class __init__() function
I understand that when a class is called it will run the code in __init__
before anything. I still don't see the difference between that, and writing the code directly under the class.
For example:
class main():
x = 1
def disp(self):
print self.x
class main():
def __init__(self):
self.x = 1
def disp(self):
print self.x
To me, both have the same functionality. (Maybe I am missing out something.) I would like to know which is more (ahem) pythonic and why.
There are a couple key differences here, both between
__init__
and writing it just under the class, as well as what you wrote.Working with
x = 1
First, you are right--these two items of code do effectively the same thing for your purposes (specifically because we're working with
int
objects here, this would be different for mutable objects):Note that they don't actually do the same thing--please see the comments on this answer for clarification.
This is why many non-standard Python libraries, like
mongoengine
anddjango
models, have a standard where you create classes without using an__init__
statement so as not to override the built-in one, but still allowing you to create class attributes, e.g., a Django example:However, as the other poster points out, there is a difference between the two in that when
x=1
is outside of the__init__
function, it is part of the class itself even when not intialized--see Zagorulkin Dmitry's answer for more detail on that. In most cases, though, that distinction won't be relevant for you.Other considerations
There are more uses for
__init__
beyond just setting variables. The most important one offhand is the ability to accept arguments during initialization. To my knowledge, there is not a way to do this without an__init__
function. I'll show you what I mean by this in this example.Let's say we're creating a
Person
class, and when we create aPerson
, we supply their age, and then their birth year is automatically calculated from that for us.In use:
This wouldn't be possible without
__init__
, since we couldn't pass the initialization theage
argument otherwise.it's different. in first example you have
x
without initialization.As already indicated, class attributes (assigned at class level) and instance attributes (assigned as a self attribute for example in
__init__
) are different.On the other hand, in your first class you are defining two different attributes (class
x
and instancex
) that coexist, but that can interfere with each other. The code below try to show the problems you can get if you define the class that way.Yes, as stated in various other questions, variables defined within the class body are attributes of the class, whereas those defined in a
def __init__(self)
block are attributes of an instance of a class.Difference between defining a member in init to defining it in the class body in Python?
Let us consider the following class definition:
In this case, we can refer x directly like:
Main.x
i.e. it is a class attribute, this belongs to every objects of this class.But, attribute
y
is specific to each object. We can not refer it directly like this:You need to instantiate an object of the class Main to refer to y:
This is similar to
static
variables in C++ and Java.