I would like to know if it can be a good idea to set certain attributes of an object only when there's a get request and the attribute was not set already. If so, is this an appropriate approach (EAFP, few lines, @property)? If not, are there best practices?
I'm setting up a GUI tests environment with lackey and unittest in python. The visual recognition by ly.Pattern()
i.e. the initialization of GUI elements takes some time so I want to do this only once and only when it's necessary.
import lackey as ly
img_path = "my_img.png"
One way
class MyClass:
@property
def foo(self):
try:
return self._foo
except AttributeError:
self._foo = ly.Pattern(img_path)
return self._foo
Another Way
class MyClass:
@property
def foo(self):
try:
return self._foo
except AttributeError:
self._foo = ly.Pattern(img_path)
return self._foo
@foo.setter
def foo(self, value):
self._foo = ly.Pattern(value)