Jonline
Jonline

Reputation: 1747

Python 2.7: How to extend a class from an imported base class

New to python, apologies if this is nub stuff:

Writing a small module (maybe a package, eventually?). It's gotten too big for single file, so I'm moving my larger classes to individual files. All of my classes extend a base class I use to house some rudimentary functions.

Here's a generic form of my problem:

runtime.py:

import baseclass
import subclass

#do stuff

baseclass.py

class BaseClass():
    def __init__(self):
        pass

    def sharedMethod(self):
        pass
        # shared functionality

subclass.py

class SubClass(baseclass.BaseClass):
    def __init__(self):
        BaseClass.__init__(self)

The traceback reads:

Traceback (most recent call last):
  File "/path/to/runtime.py", line 2, in <module>
    import baseclass, subclass
  File "path/to/subclass.py", line 2, in <module>
    class Subclass(baseclass.BassClass):
  NameError: name 'baseclass' is not defined

If I import baseclass into subclass.py it works fine, but this seems to imply that every class which extends baseclass needs to import it, and it musn't be the case that the appropriate way to do this is import baseclass over and over again.

I'd like to be able to import classes from separate files in the order that they build upon each other, I just don't know how to do it. Advice?


Note: All the answers were helpful/correct. I just chose, for the "correct" one, the answer that was most thorough/most likely to be useful to everyone. Cheers, thanks. :)

Upvotes: 1

Views: 6126

Answers (2)

BrenBarn
BrenBarn

Reputation: 251378

What you said is exactly right. You have to import baseclass in any file where you want to use it. The right way is indeed to import it "over and over" if you have many files that use it.

But that doesn't mean that every class that extends baseclass needs to import it. It just means that every module that uses baseclass needs to import it. There is no need to have just one class in a given file, and there's no particular reason to do things that way. It's perfectly fine to have import baseclass in one file and then, in that file, define several new classes all inherited from baseclass. That is a good way to do things. Don't split up each class into its own file unless each class really is big enough and complex enough to warrant that.

In addition, doing import baseclass in many files doesn't cost anything in terms of resources. The actual baseclass.py file is only run once, and the same module is re-used on later imports, so you don't pay any extra penality in terms of performance for importing it many times.

import is the way that Python files access stuff from other Python modules. You just can't use things from one file in another file without importing them.

Incidentally, why are you putting that __name__ = "SubClass" business into your modules?

Upvotes: 7

kirbyfan64sos
kirbyfan64sos

Reputation: 10727

In Python, modules behave like seperate programs. So, if you import baseclass in runtime.py, it will not be imported into subclass.py. subclass.py must import baseclass itself.

Upvotes: 0

Related Questions