在python中的类内初始化子类 [英] Initialize subclass within class in python

查看:123
本文介绍了在python中的类内初始化子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用字典在Python中初始化一个类以及两个子类.是否可以在init内检查字典中的键并根据结果初始化两个子类之一? 例如:

I am initializing a class along with two subclasses in Python using a dictionary. Would it be possible to check a key in the dictionary within the init and depending the result initialize either one of the two subclasses? For instance:

Class Pet():
    def __init__(self,dic):
       self.name=dic['name']
       self.age=dic['age']
       if dic['type']=='dog':
          #Initialize a dog which inherits all pet methods with name and age passed onto it 
       elif dic['type']=='cat':
          #Initialize a dog which inherits all pet methods with name and age passed onto it 


    def pet_methods():      
        # a bunch of pet methods that I would like to be inherited


    Class Dog():
       def __init__(self):
          self.dog_attributes=dic['dog_attributes']
    Class Cat():
       def __init__(self):
          self.cat_attributes=dic['cat_attributes']

if/else语句中可以使用什么代码?还是有更好的方法来组织代码?我很困惑,因为我似乎想在另一个init中调用一个init.

What code can be used in the if/else statement? Or is there a better way to organize the code? I am confused as it seems like I want to call a init within another init.

推荐答案

尽管通过python类的语义可以做到这一点,但我认为这是一种较差的设计模式,应避免使用.

While this may be possible by the semantics of python's classes, I would argue that this is a poor design pattern and should be avoided.

在面向对象的编程中,您的类应表示数据和逻辑各部分之间的划分.您现在正在做的是 耦合 Pet超类及其子类的逻辑/数据.相反,您应该争取的是尽可能地去耦您的对象.这简化了类之间的接口,并帮助您编写尽可能通用的类,并使用子类来实现特定的行为.

In Object Oriented programming, your classes should represent divisions between portions of your data and logic. What you're doing right now is coupling the logic/data of the Pet superclass and its subclasses. Instead, what you should strive for is to decouple your objects as much as possible. This simplifies the interface between classes and helps you to write classes that are as generic as possible and use subclasses to implement specific behavior.

对于您的情况,您应该在特定宠物的__init__方法内对不同宠物类型进行初始化.如果以这种方式编写代码,则从概念上讲,添加新的Pet子类要容易得多-您所需要做的就是从Pet继承并按原样初始化子类.

In your case, you should do the initialization of different pet types within the __init__ methods of the specific pets. If you write your code this way, it is conceptually much easier to add new Pet subclasses - all you need to do is inherit from Pet and initialize the subclass as it should be.

您现在尝试实现的方法使实现子类变得更加困难.实现者需要了解要在子类中设置哪些变量以挂钩到Pet类的初始化方案,然后它需要进入Pet源并实现用于初始化新的Pet子类类型的新功能. .这很难完成,并且需要编辑多个类才能实现新的子类.

The method that you're attempting to implement right now makes it much harder to implement subclasses. An implementer would need to understand what variables to set in the subclass to hook into the Pet classes initialization scheme, and then it would need to go into the Pet source and implement new functionality for initializing the new Pet subclass type. This is much harder to do and requires editing multiple classes just to implement a new subclass.

此问题还讨论了什么问题您正在尝试实施.

This question also talks about the problems of what you're trying to implement.

这篇关于在python中的类内初始化子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆