NOTE: My question may be more heavily based in coding convention than anything else.
I'm working on a personal project and I am looking for as clean and 'Pythonic' a way to return object variables for display.
I'm currently implementing my objects like this...
class MyChildObject(MyParentObject):
'An example object'
def __init__(self, attr_a, attr_b, attr_c):
self.attr_a = ('Attribute A', attr_a)
self.attr_b = ('Attribute B', attr_b)
self.attr_c = ('Attribute C', attr_c)
# Return a list of tuples with display names and values for each attribute
def list_attributes(self):
return [self.attr_a, self.attr_b, self.attr_c]
... (which is seems ugly and just plain wrong) and displaying their attributes with this (from MyParentObject
)...
# Displays attributes of a given object
def display_attributes(an_object):
for i, attr in enumerate(an_object.list_attributes()):
print '%s. %s: %s' % (i, attr[0], attr[1])
Setting my objects' attributes as tuples of their display and value doesn't seem right. I also thought of setting my class up like this...
class MyChildObject(object):
'An example object'
def __init__(self, attr_a, attr_b, attr_c):
self.attr_a = attr_a
self.attr_b = attr_b
self.attr_c = attr_c
# Return a list of tuples with display names and values for each attribute
def list_attributes(self):
return [('Attribute A', self.attr_a), ('Attribute B', self.attr_b), ('Attribute C', self.attr_c)]
... but that doesn't seem to be any cleaner. Is there some 'best practice' or convention for doing this?