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?
The __init__ method uses the keyword self to assign the values passed as arguments to the object attributes self. breed and self. eyeColor .
__init__ allows us to initialize this state information or data while creating an instance of the class. Here is a complete example. An instance of the class is always passed as the first argument to a method of the class.
"__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class.
The self in keyword in Python is used to all the instances in a class. By using the self keyword, one can easily access all the instances defined within a class, including its methods and attributes. __init__ is one of the reserved methods in Python. In object oriented programming, it is known as a constructor.
Yeah, check this out:
class A(object): def __init__(self): self.lst = [] class B(object): lst = []
and now try:
>>> x = B() >>> y = B() >>> x.lst.append(1) >>> y.lst.append(2) >>> x.lst [1, 2] >>> x.lst is y.lst True
and this:
>>> x = A() >>> y = A() >>> x.lst.append(1) >>> y.lst.append(2) >>> x.lst [1] >>> x.lst is y.lst False
Does this mean that x in class B is established before instantiation?
Yes, it's a class attribute (it is shared between instances). While in class A it's an instance attribute. It just happens that strings are immutable, thus there is no real difference in your scenario (except that class B uses less memory, because it defines only one string for all instances). But there is a huge one in my example.
In the first exemple you have the variable of the instance of the class. This variable is only accessible through an instance (self required).
class A(): def __init__(self): self.x = 'hello' print A.x -> AttributeError print A().x -> 'hello'
In the second exemple you have a static variable. You can access to this variable thanks to the name of the class A
class A(): x = 'hello' print A.x -> 'hello' print A().x -> 'hello'
In fact you can have a static variable and an instance variable with the same name:
class A(): x = 'hello' def __init__(self): self.x = 'world' print A.x -> hello print A().x -> world
The static value is shared between all the instances
class A(): x = 'hello' @staticmethod def talk(): print A.x a = A() print a.talk() -> hello A.x = 'world' print a.talk() -> world
You have a good article here: http://linuxwell.com/2011/07/21/static-variables-and-methods-in-python/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With