First: does it matter if i define an update function in a class or can i just do the following:
class foo(object):
def __init__(self):
self.a = 1
def f(self,):
self.a = self.a**2 - self.a - 6
return self.a
Second: is it more beneficial (save memory, runtime etc) to create a dummy variable, perform operations and use this to update my class variable, or does it not actually matter?
class foo(object):
def __init__(self):
self.a = 1
def f(self):
a = self.a
a = a**2-a-6
self.a = a
return self.a
In this trivial of an example, I know it doesn't matter, but as functions perform more operations, will it have any effect?
I cannot answer your first question because I have literally no idea what you're asking. I don't know what you mean by "an update function" or how it's different from the code you showed.
As for your second, however, using a local variable is slightly faster than using an attribute. In most cases it won't matter, but if you have some code that needs to be as fast as possible, you can indeed take the approach of moving the attribute value to a local variable, working on it there, and then putting it back in the attribute.
As Knuth said, premature optimization is the root of all evil, so don't do this until you are sure this code is causing a real performance issue.