define class over 2 files

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Tue Aug 18 01:57:46 EDT 2009


On Mon, 17 Aug 2009 21:45:57 -0700, naveen wrote:

> Is it possible to split up a class definition over multiple files?

Not exactly, but you can do variations of this:


In file A.py, create:

class Parent:
    def method(self):
        return "Method"


In file B.py, do this:

import A
class Child(B.parent):
    def another_method(self):
        return "Another Method"


Now your class Child has two methods, method() and another_method().


Similarly, you can do this:

# File A.py

def function(x, y):
    return x+y  # or something more complicated


# File B.py

import A

def MyClass(object):
    def func(self, x, y):
        return A.function(x, y)





-- 
Steven



More information about the Python-list mailing list