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.