0

What is the difference between these 2 classes (in terms of performance and design):

 class A:
        def methodA(self):
           self.a=10
           print(self.a)

And:

 class B:
        def methodB(self):
           b=10
           print(b)
  • 3
    They do completely different things. `self` is not optional, like `this` in Java. See e.g. [this question](http://stackoverflow.com/questions/2709821/python-self-explained). Hint: Move `print(self.a)` and `print(b)` to a second method and see what happens! – tobias_k Jul 04 '14 at 08:47

1 Answers1

0

b in the second example is local variable, not class-instance (or static class) variable, so with the first example you can do:

o = A()
o.methodA()
print(o.a)

With the second example this results into error and variable b runs out of scope after methodB() finishes.

About performance... By default instance variables are implemented via dictionary:

>>> class A(object):
...     def __init__(self):
...         self.a = 5
...
>>> o = A()
>>> dir(o)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__g
e__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__',
'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '_
_setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a']
>>> o.__dict__
{'a': 5}
>>> o.__dict__.__class__
<class 'dict'>

So every access is basically like doing self.__dict__['a'], performance notes here.

Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96