How to break a long string in Python?

How can I break the next line? The PEP8 directive does not make me understand.

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} 
+6
source share
4 answers

Typically, you can 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 exploits the fact that adjacent lines ( 'like ' 'this' ) are combined to shorten a long line of text; everything else is separated by commas.

+6
source
 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, } 
+6
source

There is not much point in using the dictionary literal as the right argument % . It takes up a lot of space without improving 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 the format method:

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

The way you probably most often see this breaks down into an implicit string join 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 string concatenation means that:

Expressions in parentheses, square brackets, or curly brackets can be separated by more than one physical line without the use of backslashes.

The problem with explicit joins using the \ character is that an extra character (such as a space) at the end of the physical line after \ will result in an error, for example:

 >>> 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 does not happen with an implicit join:

 >>> 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 >>> 
+2
source

Source: https://habr.com/ru/post/986822/


All Articles