I'm trying to write a short class to represent an object stored on a remote server. I expect setting attributes of the object locally without setting them on the remote server will generally be a bad idea; it'll mean the local object no longer represents the remote one, without even being aware of that fact.
I had planned on using code like the below to handle this. It lets you shoot yourself in the foot if you want to set item.name
, but assumes users will be sensible and not do so.
class Item(object):
def __init__(self):
self.name = get_item_name()
def set_name(self, name):
try:
set_item_name(name)
except:
handle_error()
raise
else:
self.name = name
However, I recently came across the @property
decorator, which let me write the below:
class Item(object):
def __init__(self):
self._name = get_item_name()
@property
def name(self):
return self._name
@name.setter
def name(self, value):
try:
set_item_name(value)
except:
handle_error()
raise
else:
self._name = value
This has the advantage of still allowing attribute access using item.name
, but also handling item.name = blah
assignment.
However, while the second block of code offers (what seems to me to be) nicer behaviour, it hides a function call in attribute setting, and given the Zen's "Explicit is better than implicit" and the general idea that Python classes should not hide things from their users, does this make the second block of code less Pythonic?