Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Wrap dòng dài trong Python

Cách ưu tiên wrap dòng dài là dùng implied line continuation của Python bên trong parentheses, brackets, braces. Nếu cần, thêm cặp parentheses quanh expression, nhưng đôi khi backslash trông đẹp hơn. Chắc chắn indent dòng tiếp tục thích hợp. Vị trí ưu tiên break quanh binary operator là sau operator, không phải trước.

Ví dụ paranthesized line break:

list(
    "Hello"
)

Output:

['H', 'e', 'l', 'l', 'o']

Ví dụ back-slashed line break:

print 'This s 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.

Cả hai đều dùng được, chọn tùy readability.