Confusing textwrap parameters, and request for RE help

Chris Angelico rosuav at gmail.com
Mon Mar 30 12:56:44 EDT 2020


On Tue, Mar 31, 2020 at 3:53 AM Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>
> On Sun, 29 Mar 2020 04:21:04 +0000, Steve Smith <Steve.A.Dore at outlook.com>
> declaimed the following:
>
> >I am having the same issue. I can either get the text to wrap, which makes all the text wrap, or I can get the text to ignore independent '/n' characters, so that all the blank space is removed. I'd like to set up my code, so that only 1 blank space is remaining (I'll settle for none at this point), an the text wraps up to 100 chars or so out per line. Does anyone have any thoughts on the attached code? And what I'm not doing correctly?
> >
> >
> >#import statements
> >import textwrap
> >import requests
> >from bs4 import BeautifulSoup
> >
> >#class extension of textwrapper
> >class DocumentWrapper(textwrap.TextWrapper):
> >
> >    def wrap(self, text):
> >        split_text = text.split('\n')
> >        lines = [line for para in split_text for line in textwrap.TextWrapper.wrap(self, para)]
>
>         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, 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.

Is that what you were intending, Steve?

ChrisA


More information about the Python-list mailing list