In python, I know that looking up a locally scoped variable is significantly faster than looking up a global scoped variable. So:
a = 4
def function()
for x in range(10000):
<do something with 'a'>
Is slower than
def function()
a = 4
for x in range(10000):
<do something with 'a'>
So, when I look at a class definition, with an attribute and a method:
class Classy(object):
def __init__(self, attribute1):
self.attribute1 = attribute1
self.attribute2 = 4
def method(self):
for x in range(10000):
<do something with self.attribute1 and self.attribute2>
Is my use of self.attribute more like my first or second function? What about if I sub class Classy, and try to access attribute2 from a method in my sub class?