Virgiliu
Virgiliu

Reputation: 3118

How to correctly break a long line in Python?

How would I go about breaking the following line? The PEP8 guideline doesn't make it very clear to me.

confirmation_message = _('ORDER_CREATED: %(PROPERTY_1)s - %(PROPERTY_2)s - %(PROPERTY_3)s - %(PROPERTY_4)s')  % {'PROPERTY_1': order.lorem, 'PROPERTY_2': order.ipsum, 'PROPERTY_4': order.dolor, 'PROPERTY_5': order.sit}

Upvotes: 6

Views: 986

Answers (4)

chepner
chepner

Reputation: 530902

There's not much point in using a dictionary literal as the right-hand argument of the %. It takes up a lot of space without really enhancing the readability of your code.

confirmation_message = _('ORDER_CREATED: %s - %s - %s - %s') % (
    order.lorem, 
    order.ipsum, 
    order.dolor,
    order.sit
)

The operator.attrgetter function is useful here:

import operator
attrs = operator.attrgetter('lorem', 'ipsum', 'dolor', 'sit')
confirmation_message = _('ORDER_CREATED: %s - %s - %s - %s') % attrs(order)

If you can, switch to using the format method:

msg_template = 'ORDER_CREATED: {0.lorem} - {0.ipsum} - {0.dolor} - {0.sit}'
confirmation_message = _(msg_template).format(order)

Upvotes: 4

Deacon
Deacon

Reputation: 3803

The way you'd probably see this broken up the most often is using implicit line joining, similar to this:

confirmation_message = (_('ORDER_CREATED: %(PROPERTY_1)s - %(PROPERTY_2)s - '
                          '%(PROPERTY_3)s - %(PROPERTY_4)s')  %
                        {'PROPERTY_1': order.lorem, 'PROPERTY_2': order.ipsum,
                         'PROPERTY_4': order.dolor, 'PROPERTY_5': order.sit})

Implicit line joining means that:

Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes.

The problem with explicit joining using the \ character is that an extra character (like a space) at the end of the physical line after the \ will cause an error, like so:

>>> c = '1 2 3 4 5 6 ' \  # This will generate an error.
  File "<stdin>", line 1
    c = '1 2 3 4 5 6 ' \  # This will generate an error.
                                                       ^
SyntaxError: unexpected character after line continuation character
>>>

This doesn't happen with implicit joining:

>>> c = ('1 2 3 4 5 6 ' # No error.
...      '7 8 9 10')
>>> print(c)
1 2 3 4 5 6 7 8 9 10
>>>

Upvotes: 2

Andrea Corbellini
Andrea Corbellini

Reputation: 17751

confirmation_message = (
    _(
        'ORDER_CREATED: %(PROPERTY_1)s - %(PROPERTY_2)s - '
        '%(PROPERTY_3)s - %(PROPERTY_4)s'
    ) % {
        'PROPERTY_1': order.lorem,
        'PROPERTY_2': order.ipsum,
        'PROPERTY_4': order.dolor,
        'PROPERTY_5': order.sit,
    })

Or also:

confirmation_message_tmpl = _(
    'ORDER_CREATED: %(PROPERTY_1)s - %(PROPERTY_2)s - '
    '%(PROPERTY_3)s - %(PROPERTY_4)s'
)
confirmation_message = confirmation_message_tmpl % {
    'PROPERTY_1': order.lorem,
    'PROPERTY_2': order.ipsum,
    'PROPERTY_4': order.dolor,
    'PROPERTY_5': order.sit,
}

Upvotes: 6

larsks
larsks

Reputation: 311318

One would typically do something like:

confirmation_message = _(
    'ORDER_CREATED: %(PROPERTY_1)s - '
    '%(PROPERTY_2)s - %(PROPERTY_3)s - %(PROPERTY_4)s') % {
        'PROPERTY_1': order.lorem,
        'PROPERTY_2': order.ipsum,
        'PROPERTY_3': order.ipsum,
        'PROPERTY_4': order.dolor,
        'PROPERTY_5': order.sit
    }

This takes advantage of the fact that adjacent strings ('like ' 'this') are concatenated to shorten the long line of text; everything else is split on commas.

Upvotes: 6

Related Questions