Long Lines Wrap करना
Long Lines Wrap करने का Preferred तरीका Python के Implied Line Continuation का उपयोग करना है Parentheses, Brackets और Braces के अंदर। यदि आवश्यक हो, तो आप Expression के चारों ओर Parentheses की Extra Pair Add कर सकते हैं, लेकिन कभी-कभी Backslash बेहतर दिखता है। Continued Line को Appropriately Indent करना सुनिश्चित करें। Binary Operator के आसपास Break करने की Preferred जगह Operator के बाद है, उससे पहले नहीं।
Parenthesized Line Break का उदाहरण:
my_list = (
"Hello"
)
यह Output देगा:
['H', 'e', 'l', 'l', 'o']
Backslash Line Break का उदाहरण:
print('This is a really long line,', \
'but we can make it across multiple lines.')
यह Output देगा:
This is a really long line, but we can make it across multiple lines.
Best Practices (PEP 8):
1. Implicit Line Continuation (Preferred):
# Parentheses
result = (first_part +
second_part +
third_part)
# Brackets
my_list = [
item1,
item2,
item3,
]
# Braces
my_dict = {
'key1': value1,
'key2': value2,
}
# Function Calls
function_call(
arg1,
arg2,
arg3,
)
2. Explicit Line Continuation (Backslash) - जब जरूरी हो:
# With Statement Multiple Context Managers के साथ
with open('file1.txt') as f1, \
open('file2.txt') as f2:
pass
# Assert Statements
assert (condition1 and
condition2 and
condition3)
3. String Concatenation:
# Implicit (Preferred)
message = (
"This is a very long message "
"that spans multiple lines "
"without any operator."
)
# Explicit
message = "This is a very long message " \
"that spans multiple lines " \
"with explicit continuation."