You can override __getattribute__
to do that:
class Thingy:
def __init__(self):
self.data = ['huey', 'dewey', 'louie']
self.other = ['tom', 'jerry', 'spike']
def __getattribute__(self, attr):
if attr == 'data':
return ' '.join(super().__getattribute__(attr))
else:
return super().__getattribute__(attr)
print(Thingy().data)
print(Thingy().other)
Output:
huey dewey louie
['tom', 'jerry', 'spike']
Python 2 version:
class Thingy(object):
def __init__(self):
self.data = ['huey', 'dewey', 'louie']
self.other = ['tom', 'jerry', 'spike']
def __getattribute__(self, attr):
if attr == 'data':
return ' '.join(super(Thingy, self).__getattribute__(attr))
else:
return super(Thingy, self).__getattribute__(attr)
print(Thingy().data)
print(Thingy().other)
Note that it is easy to get into infinite loops with overriding __getattribute__
, so you should be careful.
There's almost certainly a less scary way to do this, actually, but I can't think of it right now.