根据 PEP8 准则,我将 url 缩短为短长度时出现意外缩进错误

Unexpected indent error while i break my url to short length as per PEP8 guideline

我试图使 url 的长度变短,但它给出了意想不到的 indent.i 提供了它给出错误

的代码段

引用:

回溯(最后一次调用): 文件 "C:\Users\HOME\Desktop\movie trailer\entertainment.py",第 1 行,位于 进口媒体 文件 "C:\Users\HOME\Desktop\movie trailer\media.py",第 85 行 "/w185"+str(detail_new[4])" ^ IndentationError:意外缩进

self.poster_image_url = "http://image.tmdb.org/t/p"
                                "/w185"+str(detail_new[4])"
self.trailer_youtube_url = 
                    "https://www.youtube.com/watch?"
                    "v="+str(self.get_trailer_link(movie_name))"

您应该使用“\”向Python表明该行没有终止:

my_variable = "beginning of the string" \
              "end of the string"

对于你的第二种情况:

my_variable = \
  "beginning of the string" \
  "end of the string"

您也可以使用括号来达到同样的目的:

my_variable = (
  "beginning of the string"
  "end of the string"
)

针对您的具体情况:

self.poster_image_url = (
  'http://image.tmdb.org/t/p'
  '/w185' + str(detail_new[4])
)
self.trailer_youtube_url = (
  'https://www.youtube.com/watch?'
  'v=' + str(self.get_trailer_link(movie_name))
)
self.poster_image_url = "http://image.tmdb.org/t/p  \
                                         /w185+str({0})])".format(detail_new[4])

self.trailer_youtube_url = "https://www.youtube.com/watch?" \
                                               "v=" + "str({0})".format(self.get_trailer_link(movie_name))