I want understand how this word wrap program playing on input

Arup Rakshit ar at zeit.io
Thu Apr 4 14:33:12 EDT 2019


I am reading a Python book, where the author used a simple word wrap program to explain another concept. But I am not understanding some parts of the program.

    def wrap(text, line_length):
        """Wrap a string to a specified line length"""
        words = text.split()
        lines_of_words = []
        current_line_length = line_length
    
        for word in words:
            if current_line_length + len(word) > line_length:
                lines_of_words.append([]) # new line
                current_line_length = 0
            lines_of_words[-1].append(word)
            current_line_length += len(word)
    
        lines = [' '.join(line_of_words) for line_of_words in lines_of_words]
        return '\n'.join(lines)
    
    wealth_of_nations = "The annual labour of every nation is the fund which or" \
    "iginally supplies it with all the necessaries and conveniencies of life wh" \
    "ich it annually consumes, and which consist always either in the immediate" \
    " produce of that labour, or in what is purchased with that produce from ot" \
    "her nations. According, therefore, as this produce, or what is purchased w" \
    "ith it, bears a greater or smaller proportion to the number of those who a" \
    "re to consume it, the nation will be better or worse supplied with all the" \
    " necessaries and conveniencies for which it has occasion."

Now when I call it:

    python3 -i wrapper.py 
    >>> wrap(wealth_of_nations, 25)
    'The annual labour of every\nnation is the fund which\noriginally supplies it with\nall the necessaries and\nconveniencies of life which\nit annually consumes, and\nwhich consist always either\nin the immediate produce of\nthat labour, or in what is\npurchased with that produce\nfrom other nations.\nAccording, therefore, as\nthis produce, or what is\npurchased with it, bears a\ngreater or smaller\nproportion to the number of\nthose who are to consume it,\nthe nation will be better or\nworse supplied with all the\nnecessaries and\nconveniencies for which it\nhas occasion.'
    >>> print(_)
    The annual labour of every
    nation is the fund which
    originally supplies it with
    all the necessaries and
    conveniencies of life which
    it annually consumes, and
    which consist always either
    in the immediate produce of
    that labour, or in what is
    purchased with that produce
    from other nations.
    According, therefore, as
    this produce, or what is
    purchased with it, bears a
    greater or smaller
    proportion to the number of
    those who are to consume it,
    the nation will be better or
    worse supplied with all the
    necessaries and
    conveniencies for which it
    has occasion.

My questions are:

1. why  `line_length` is set to `current_line_length` initially?
2. In the if clause why `current_line_length` is set back to 0 every time the condition is true.
3. Also why that `if` test is required there.

Can anyone help me to understand this program, I am totally confused.

Thanks,

Arup Rakshit
ar at zeit.io






More information about the Python-list mailing list