Instance Inheritance – Python (My Project Cont’d)

What puzzled me at first was how to not only make a class inherit attributes from a parent class but how to have an instance of a class inherit the instantiated attribute data from a parent.

EXAMPLE (also see last post):

I have a PROJECT parent class.

class PROJECT:
    def __init__(self, projectnumber, Title=””,Title2=””,Name=”” … ):

is instantiated as newproject = PROJECT()

and I have a POINT (borehole) child class.

    def __init__(self, name=””, depth=”” ….):

Now, I would like to create boreholes, i.e. instantiate the POINT class, for this project and have each POINT instance inherit not only the PROJECT attributes but the information for the object newproject.

I found the solutiom here on (great site !). And after adapting it to my project, I came up with his.

class PROJECT:
    def __init__(self, Title=””,Title2=””,  …more attributes … )

    _inherited = [‘Title’,’Title2′,’Name’ …more attributes …  ]

    def __init__(self, project, name):
        self._parent = project = name
    def __getattr__(self, name ):
        if name in self._inherited:
            return getattr(self._parent, name)

        return self.__dict__[name]


Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s