How to inherit multiple instances of a class from a single class in python

旧街凉风 提交于 2020-03-23 14:20:08

问题


Disclaimer: I am new to programming and have just started learning about Classes and Inheritance so perhaps it is my lack of understanding which is causing me issues?

I have created two classes in seperate files person.py and address.py.

The person class inherits from the address class however a person can have multiple addresses (postal, physical etc..) How can I inherit multiple instances of my address class to for each type.

My goal is to eventually have a library of common classes for future projects so any help or constructive comments around this concept are much appreciated.

I want to be able to create something like the below code where both the postal and the physical address utilise the Address class:

employee = Person()
employee.set_postal_address("342 Gravelpit Terrace","Bedrock")
employee.set_physical_address("742 Evergreen Tce", "Springfield")

Below is my code, I have not created any reference to Address() in the Person() class as I am not sure how to achieve this?

# address.py

class Address:
    def __init__(self):
        self.__line1 = None
        self.__line2 = None
        self.__town_city = None
        self.__post_code = None
        self.__state_region = None
        self.__country = None

    # Line 1
    def get_line1(self):
        return self.__line1

    def set_line1(self, line1):
        self.__line1 = line1

    #etc....
# person.py

from address import * 

class Person(Address):

    def __init__(self):
        self.__first_name = None
        self.__last_name = None
        self.__postal_address = []
        self.__physical_adress = []


    def set_postal_address(self):
        # use the address class 
        # return a list of address values
        self.__postal_address = []

    def set_physical_address(self):
        # use the address class 
        # return a list of address values
        self.__physical_address = []  

    #etc...

Any help is greatly appreciated. If there is a better way of handling this please let me know.


回答1:


You do not need to necessarily inherit Address when creating the class Person. One way to achieve your goal is to initialize an Address class object within the Person class.

# address.py

class Address:
    def __init__(self, **kwargs):
        self.__line1 = kwargs.get('__line1')
        self.__line2 = kwargs.get('__line2')
        self.__town_city = kwargs.get('__town_city')
        self.__post_code = kwargs.get('__post_code')
        self.__state_region = kwargs.get('__state_region')
        self.__country = kwargs.get('__country')

    # Line 1
    def get_line1(self):
        return self.__line1

    def set_line1(self, line1):
        self.__line1 = line1

    #etc....
# person.py

from address import * 

class Person:

    def __init__(self, **kwargs):
        self.__first_name = kwargs.get('__first_name')
        self.__last_name = kwargs.get('__last_name')
        self.__postal_address = None
        self.__physical_address = None


    def set_postal_address(self, **kwargs):
        # use the address class 
        self.__postal_address = Address(**kwargs)

    def set_physical_address(self, **kwargs):
        # use the address class 
        self.__physical_address = Address(**kwargs)

    #etc...

This allows you to keep inherited methods from getting cluttered in a given class object but still allows you to keep nice class structures.

Then you can do something like:

bob=Person(__first_name='Bob', __last_name='Smith')
bob.set_postal_address(__line1='42 Wallaby Way', __town_city='Sydney')
# Since we are using kwargs.get() any not specified Kwarg comes out as None.
# Now `__postal_address` is its own Address class and comes with Address methods
bob.__postal_address.set__line2('Apt 3B')

Now there are times when you would want to inherit common methods from higher level classes. You can read more on python classes here: (https://docs.python.org/3.6/tutorial/classes.html)



来源:https://stackoverflow.com/questions/59329157/how-to-inherit-multiple-instances-of-a-class-from-a-single-class-in-python

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