Making helper methods more concise
Terry Reedy
tjreedy at udel.edu
Mon Apr 16 12:17:52 EDT 2012
On 4/16/2012 8:01 AM, Alan Ristow wrote:
> Hi all,
>
> I have defined a class that includes a number of helper methods that
> are useful to me, but pretty redundant. Something like so, where I
> maintain a list of tuples:
>
> class A(object):
> def __init__(self):
> self.listing = []
>
> # This method does the work.
> def append_text(self, text, style):
> self.listing.append((text, style))
The order of parameters and tuples seems backwards to me. Suppose you
want to sort the list by style ;-). But maybe you never will. 'append'
as the method name would seem sufficient to me.
> # The rest of the methods are just helpers.
> def append_paragraph(self, text):
> self.append_text(text, 'Paragraph')
>
> def append_header(self, text):
> self.append_text(text, 'Header')
>
> def append_title(self, text):
> self.append_title(text, 'Title')
>
> obj = A()
> obj.append_title('On Learning Something New About Python')
> obj.append_header('A Question Is Posed')
> obj.append_paragraph('Where is lorem ipsum when you need it?')
I would try a different approach:
T = 'Title'
H = 'Header'
P = 'Paragragh'
Then dump all the reverse-curried helpers.
obj.append(T, 'On Learning Something New About Python')
obj.append(H, 'A Question Is Posed')
obj.append(P, 'Where is lorem ipsum when you need it?')
--
Terry Jan Reedy
More information about the Python-list
mailing list