Say there is a class:
class x(obj):
y = 1
What is faster (or preferred):
def __init__(self):
print self.y
or:
def __init__(self):
print x.y
I assume x.y better communicates the intend but I’m interested in the speed implications.
The performance gain you could possibly achieve with these micro optimizations doesn’t matter. The impact of the
printing dwarfs the cost of attribute access by far. For reference, here’s a test script:On my machine (with the yakuake terminal), this outputs
This is within experimental error of both variants being identical. Crude experimentation shows that:
Therefore, it’s safe to say to derive the conclusion that there is no difference in performance.