简体   繁体   English

一行末尾的反斜杠有什么用?

[英]What is the purpose of a backslash at the end of a line?

Just found the following module import in a Python code:刚刚在Python代码中发现了以下模块导入:

from sqlalchemy.ext.declarative import declarative_base,\
      AbstractConcreteBase

I am curious about the backslash \ at the end of the first line.我对第一行末尾的反斜杠\很好奇。 What's the purpose of it?它的目的是什么? Wouldn't it be the same as the following?不就和下面的一样吗?

from sqlalchemy.ext.declarative import declarative_base, AbstractConcreteBase

Yep, it's exactly the same and this is the point of the backslash — it escapes the newline, allowing this long line to be split in two.是的,它完全一样,这就是反斜杠的重点——它转义了换行符,允许将这条长行一分为二。 An alternative is to use parentheses:另一种方法是使用括号:

from sqlalchemy.ext.declarative import (declarative_base,
      AbstractConcreteBase)

While this is a syntax error:虽然这是一个语法错误:

from sqlalchemy.ext.declarative import declarative_base,
      AbstractConcreteBase

In the context of line-oriented text, especially source code for some programming languages, it is often used at the end of a line to indicate that the trailing newline character should be ignored, so that the following line is treated as if it were part of the current line.在面向行的文本的上下文中,尤其是某些编程语言的源代码,它通常用在行尾以指示应忽略结尾的换行符,以便将下一行视为部分当前行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM