Can I run an operation on an object's attribute when reading?
clp2 at rebertia.com
Mon Jan 19 13:15:05 CET 2009
On Mon, Jan 19, 2009 at 4:04 AM, Phillip B Oldham
<phillip.oldham at gmail.com> wrote:
> Is it possible to automatically run an operation on a object's
> attribute when reading? For instance, if I have the following:
> class Item(object):
> tags = ['default','item']
> item = Item()
> desc = item.tags
> When I'm reading the item.tags, I'd like to automagically have the
> value converted to a string, eg: "default item". I know I could write
> a getter to do this for me, but I'd like to avoid that if possible on
> this occasion.
Assuming I'm interpreting you correctly (you're going to have to use
something like a getter):
self._tags = ['default', 'item']
return ' '.join(self._tags)
print Item().tags #==> default item
Follow the path of the Iguana...
More information about the Python-list