Python Self Keyword: Advantages And Drawbacks
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: cla
Solution 1:
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:
>>> classA(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.
Post a Comment for "Python Self Keyword: Advantages And Drawbacks"