The performance gain you could possibly achieve with these micro optimizations doesn't matter. The impact of the print
ing dwarfs the cost of attribute access by far. For reference, here's a test script:
import sys,timeit
class ClassAccess(object):
y = 1
def __init__(self):
print(ClassAccess.y)
class SelfAccess(object):
y = 1
def __init__(self):
print(self.y)
ca = timeit.timeit(ClassAccess, number=100000)
sa = timeit.timeit(SelfAccess, number=100000)
sys.stderr.write(str(ca) + "\n")
sys.stderr.write(str(sa) + "\n")
On my machine (with the yakuake terminal), this outputs
0.640013933182
0.628859043121
This is within experimental error of both variants being identical. Crude experimentation shows that:
- Approximately 90% of the runtime is caused by actually displaying the printed result.
- Of the rest, approximately 50% is the time that the print statement alone takes up.
- Approximately 80% of the rest of that is caused by the allocation of the objects.
Therefore, it's safe to say to derive the conclusion that there is no difference in performance.