Confusing textwrap parameters, and request for RE help

Peter Otten __peter__ at web.de
Mon Mar 30 17:18:50 EDT 2020


Chris Angelico wrote:

[Steve]

>>>    def wrap(self, text):
>>>        split_text = text.split('\n')
>>>        lines = [line for para in split_text for line in 
textwrap.TextWrapper.wrap(self, para)]

[Dennis]
>>         That... Looks rather incorrect.
>>
>>         In most all list-comprehensions, the result item is on the left, 
and
>> the rest is the iteration clause... And why the (self, ...)? You 
inherited
>> from textwrap.TextWrapper, yet here you bypass the inherited to invoke 
the
>> wrap method (without instantiating it!).

> I think this is a reasonable thing to do, 

Indeed, Steve's trying to apply the superclass algo on every paragraph of 
the text.

> but an awkward way to spell it. If you mean to call the original wrap
> method, it would normally be spelled super().wrap(para) instead.

Probably a workaround because super() cannot do its magic in the list 
comprehensions namespace. Another workaround is to define

wrap = super().wrap

and then use just wrap() in the listcomp.



More information about the Python-list mailing list