Inheritance vs using a class object of one class as a field of another class (Python 3)

十年热恋 提交于 2019-12-11 18:17:41

问题


Say we have a product which is identifiable by an ID number and a Quality field which is in turn comprised of several values, one of which is 2d dimensions values. Is this a case of inheritance or just using an object as another classes' field?

In trying to implement the above in Python using OOP and classes I thought of the following:

class Dimensions:

    def __init__(self, x, y):
        self.x = x
        self.y = y

class Quality:

    def __init__(self, val1, val2, dimensions):
        self.val1 = val1
        self.val2 = val2
        self.dimensions = Dimensions

class Product:
    def __init__(self, id, quality):
        self.id = id
        self.quality = Quality

Is what I am trying to do above achieved via inheritance?

e.g. class Product (Quality):

If so, how it would look like in defining the classes above in Python and how would I be passing the fields of a specific instance of a parent class (assuming Dimensions is the parent of Quality etc.)?

Perhaps my question is naive but I am trying to understand Python's approach of Class inheritance and structure; searched online for examples in Python but could not find one to help me understand. If this has an answer elsewhere, please share the link or, instead, point me to the right direction.

Completing the example above in code from defining the classes and instantiating each of them would be much helpful.


回答1:


Is what I am trying to do above achieved via inheritance?

Inheritance describes a "is a" relationship so the question are: 1/ is a Product a Quality ? and 2/ is a Product a Dimension ?

Given your very own words:

we have a product which is identifiable by an ID number and a Quality field which is in turn comprised of several values, one of which is 2d dimensions values

the answer to both questions is a clear "NO" : your product "is" not a quality, it "has" a quality. And it's not a dimension, it's the quality which has a dimension. IOW, you want composition (what you already have in your snippet), not inheritance.

Perhaps my question is naive but I am trying to understand Python's approach of Class inheritance and structure

This is nothing python-specific, just basic OO design. The first part of the famous "Design patterns" book (the original one) is possibly one of the best texts I know about OO design.

If you could also complete my code snippet to illustrate how class composition (definition could be illustrated in this example? e.g. when instantiating a Product instance, will I provide the values for the other classes it is composed from?

There's no one-size-fits-all rule here, it depends on the context, and specially on the various objects lifecycle. In it's simplest form, you explicitely instanciate each objects and pass them to each other, ie:

class Dimensions:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Quality:
    def __init__(self, val1, val2, dimensions):
        self.val1 = val1
        self.val2 = val2
        self.dimensions = dimensions

class Product:
    def __init__(self, id, quality):
        self.id = id
        self.quality = quality


dimensions = Dimensions(1, 2)
quality = Quality("A", "B", dimensions)
product = Product(42, quality)

At the other end of the spectrum, you pass all "raw" values to the product, which creates it's quality, which defines it's dimensions:

class Dimensions:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Quality:
    def __init__(self, val1, val2, x, y):
        self.val1 = val1
        self.val2 = val2
        self.dimensions = Dimensions(x, y)

class Product:
    def __init__(self, id, val1, val2, x, y):
        self.id = id
        self.quality = Quality(val1, val2, x, y)


product = Product(42, "A", "B", 1, 2)

and you can of course use any variant, and even use them all providing alternate constructors. The main forces here is are whether dimensions and/or quality should have a life outside products, and whether they should or not be shared amongst products.



来源:https://stackoverflow.com/questions/54475380/inheritance-vs-using-a-class-object-of-one-class-as-a-field-of-another-class-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!