我为班级电影制作了第一个文件夹:
import webbrowser
class Movie():
def __init__(self,movie_title,movie_storyline,poster_image,trailer_youtube):
self.title=movie_title
self.storyline=movie_storyline
self.poster_image_url=poster_image
self.trailer=trailer_youtube
def show_trailer(self):
webbrowser.open(self.trailer)
然后在另一个文件中,我试图调用该函数:
import media
toy_story= media.Movie("toy_story","a boy who have toys","http://www.imdb.com/title/tt0114709/mediaviewer/rm3813007616","http://www.imdb.com/videoplayer/vi2052129305?playlistId=tt0114709&ref_=tt_ov_vi")
avatar=media.Movie("Avatar","A marine on an alien planet","http://www.imdb.com/title/tt0499549/mediaviewer/rm843615744","http://www.imdb.com/title/tt0499549/videoplayer/vi531039513?ref_=tt_ov_vi")
avatar.show_trailer()
但是,我收到了以下错误:
AttributeError: Movie instance has no attribute 'show_trailer'
有任何建议吗?
答案 0 :(得分:0)
我认为你的班级{1}}的缩进是错误的。我无法使用同一文件夹中的以下2个文件重现问题 - 也许您的方法没有正确缩进成为该类的一部分:
<强> media.py 强>
Movie
<强> test.py 强>
import webbrowser
class Movie():
def __init__(self,movie_title,movie_storyline,poster_image,trailer_youtube):
self.title = movie_title
self.storyline = movie_storyline
self.poster_image_url = poster_image
self.trailer = trailer_youtube
def show_trailer(self):
print(self.trailer)
输出:
import media
toy_story=media.Movie("toy_story",
"a boy who have toys",
"http://www.imdb.com/title/tt0114709/mediaviewer/rm3813007616",
"http://www.imdb.com/videoplayer/vi2052129305?playlistId=tt0114709&ref_=tt_ov_vi")
avatar=media.Movie("Avatar",
"A marine on an alien planet",
"http://www.imdb.com/title/tt0499549/mediaviewer/rm843615744",
"http://www.imdb.com/title/tt0499549/videoplayer/vi531039513?ref_=tt_ov_vi")
avatar.show_trailer()