简体   繁体   English

Python继承__init__混乱

[英]python inheritance __init__ confusion

I am using python 2.7 and confused about inheritance concept. 我正在使用python 2.7并对继承概念感到困惑。

I want to make a script that can download from various manga (japanese comic site) 我想制作一个可以从各种漫画下载的脚本(日本漫画网站)

Every manga site uses a different method to save their file. 每个漫画站点都使用不同的方法来保存其文件。 So i have a "Manga_site" class and class called "mangacanblog" (this is a website manga). 因此,我有一个“ Manga_site”类和一个名为“ mangacanblog”的类(这是一个网站漫画)。

Every manga site has a: homepage,collection,page,etc. 每个漫画站点都有一个主页,收藏集,页面等。 and it's all different for each site. 每个站点都不同。 my question is. 我的问题是。 is this script correct to store those homepage,collection_page,etc variable? 这个脚本是否正确存储这些主页,collection_page等变量? or should I use self.homepage in mangacanblog class and not in the Manga_site class? 还是应该在mangacanblog类中而不是在Manga_site类中使用self.homepage?

class Manga_site:

    def __init__(self,homepage,collection_page,base_manga_page,manga_title = ""):
        self.homepage = homepage
        self.collection_page = collection_page
        self.base_manga_page = base_manga_page
        self.manga_title = manga_title


class Mangacanblog(Manga_site):

    def __init__(self,manga_title):
        homepage = bloglink
        collection_page = collectionpagelink
        manga_title = manga_title.lower()
        base_manga_page = basepagelink

In your Mangacanblog 's __init__() function you are just setting local variables homepage , collection_page , etc, not instance variables, to make them instance variables you have to use self. 在您的Mangacanblog__init__()函数中,您只是设置局部变量homepagecollection_page等,而不是实例变量,以使其成为您必须使用self.实例变量self. , and this needs to be used in both super class as well as subclass. ,这需要在超类和子类中使用。

But a better thing to do would be to call super(Manga_site).__init__() to let the super class handle its initialization, and then you can initialize sub class just the way you want to. 但是更好的方法是调用super(Manga_site).__init__()来让超类处理其初始化,然后您可以按照自己的方式初始化子类。

But for this, you would need to define Manga_site as a new style class, by inheriting from object class. 但是为此,您需要通过从object类继承来将Manga_site定义为新样式类。 Example - 范例-

class Manga_site(object):

    def __init__(self,homepage,collection_page,base_manga_page,manga_title = ""):
        self.homepage = homepage
        self.collection_page = collection_page
        self.base_manga_page = base_manga_page
        self.manga_title = manga_title


class Mangacanblog(Manga_site):

    def __init__(self,manga_title):
        super(Mangacanblog, self).__init__(bloglink, collectionpagelink, basepagelink, manga_title.lower())

I have guessing you would define bloglink and collectionpagelink , before you use them in the subclass. 我猜想您将在子类中使用它们之前定义bloglinkcollectionpagelink


But this given, from the example you have given, what you are trying to achieve may be to use objects of class, and not inheritence. 但是,从给出的示例来看,给出的结果可能是要使用类的objects ,而不是继承。

Inheritence is used when the subclass has extra attributes/properties and extra methods. 当子类具有额外的属性/属性和额外的方法时,将使用继承。

You create different objects of same class if you want to store different data in them. 如果要在它们中存储不同的数据,则可以创建同一类的不同对象。 Example - 范例-

class Manga_site:

    def __init__(self,homepage,collection_page,base_manga_page,manga_title = ""):
        self.homepage = homepage
        self.collection_page = collection_page
        self.base_manga_page = base_manga_page
        self.manga_title = manga_title

mangacanblog = Manga_site('<homepage url>','<collection_page>',....)
anothermangasite = Manga_site('<another url>',....)

Then you can use these objects anyway you want to. 然后,您可以随时使用这些对象。

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

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