They are not the same. In your example it was almost mere coincidence. Just do not instantiate the class c
who will see what happens.
This is because classes are only a scope limiter in Python. You can enter codes normally within the class, not only definitions of fields and methods, but also occur in other languages. The difference is that this code will execute when the class is defined.
class c():
t = "abc"
print (t)
for a in range (3):
print (a)
This will already show in the terminal even if the class is not instantiated, different from the class d
, that implements the initializer method, which will have the logic executed only when instantiated.
You can use this to define compatibility with different versions of Python. Let’s say that a method has functionalities that vary according to the version to be executed and you intend to maintain support for both, could do something like:
import sys
class Foo:
if sys.version_info[0] == 3:
def something(self):
return 'Python 3'
else:
def something(self):
return 'Python 2'
foo = Foo()
print(foo.something())
See working on Python 3 and in the Python 2
Obviously we would not do something of this kind for such a simple method, but when you start working with some more differences drastic between versions, such as using the method __str__
or __unicode__
, this approach starts to make sense.
Not to mention the difference that when you define a field without associating to self
, you will be defining a class attribute, while when used next to the self
will be an instance attribute. This will make all the difference when the type worked is changeable, because being mutable and being class attribute, once modified the modification will be reflected in all instances.
class Foo:
field = []
a = Foo()
a.field.append(1)
print(a.field) # [1]
b = Foo()
print(b.field) # [1]
See working on Repl.it
Already, if used within __init__
, with the self
, the exit of b.field
would be []
, would be an independent list of a.field
, even if they are instances of the same class. In your case, c.t
will be a class attribute while d.t
will be an instance attribute.
That? https://answall.com/q/109013/101
– Maniero
@Maniero, I read these articles, but I still don’t understand why to use
__init__
if in my example the results are the same?– Rogério Dec