Reputation: 1600
I'm curious about the recommended way to return a value for instance methods in Python. I'm sorry if this comes across as a rather trivial question, but I can't seem to find a proper answer on google/sx/so.
Option 1:
class Cla:
def __init__(self):
self.var = False
def _inst_mtd(self):
var = True
return var
def calc_var(self):
self.var = self._inst_mtd() # or in init if desired
def return_var(self):
return self.var
Option 2:
class Cla:
def __init__(self):
self.var = False
def _inst_mtd(self):
self.var = True
def calc_var(self):
self._inst_mtd()
def return_var(self):
return self.var
Option 3:
class Cla:
def __init__(self):
self.var = False
def _inst_mtd(self):
self.var = True
return self.var
def calc_var(self):
self.var = self._inst_mtd()
def return_var(self):
return self.var
The intention of _inst_mtd
is to calculate a value for self.var
. A separate public method to return self.var
will provide the return
function to an outside caller . _inst_mtd
is meant to be called within the class, perhaps in a loop, and perhaps with a public method calc_var
if for instance it'll take a while to run .
My concerns are that 1 might result in confusion between local and instance variables, if the whole point of _inst_mtd
is to modify self.var
and not actually deal with another variable called var
. For 2, I'm under the impression that (considering only _inst_mtd
) this constitutes a side-effect, which should be avoided? But 3 just seems unnecessary, although it does work well with return type annotations, especially when the type (not recommended) or elements within are changed in _inst_mtd
.
Upvotes: 0
Views: 1999
Reputation: 3880
Of the three, I think Option 2 makes most sense, since each method is clear in its role. But if _inst_mtd
really doesn't need the internal state, as in Option 1, then you should probably just make it a function outside of the class. In Python, using getters just in case you might someday need them is generally considered the wrong way, because it is a superfluous complication most of the time. Also, you can always extract the internally-used method from the public method if and when needed. Simplicity where you can keep it is usually worth more than being engineered for hypothetical future needs.
class Cla:
def __init__(self):
self.var = False
def calc_var(self):
self.var = True
instance = Cla()
instance.calc_var()
value = instance.var
Upvotes: 2
Reputation: 2363
You are putting too much emphasis on public/private access. In Python every member of any instance (method, variable, etc.) is publicly available. So, you wouldn't even usually bother to write special getter
or setters
. Instead:
c = Cla()
c.var = 42
print(c.var)
is perfectly acceptable. If you still would like to hint to other programmers that the variable should be private and not messed with, you would usually prefix it with a single underscore: self._var
. However, this is only a convention. For more, see https://stackoverflow.com/a/1301369/1269892
Upvotes: 2