By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,544 Members | 1,437 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,544 IT Pros & Developers. It's quick & easy.

Inheriting Classes Across Files

P: n/a
In my program I need derived classes to inherit from the super class.

I can do this but I'd like to split the super class into it's own file.

How do you do this? When I split the code I start getting errors.

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Tina" <bad_addy@no_domain.com> wrote in message
news:E6***************@newsread2.news.pas.earthlin k.net...
In my program I need derived classes to inherit from the super class.

I can do this but I'd like to split the super class into it's own file.

How do you do this? When I split the code I start getting errors.
The module with the superclass has to be imported before
the subclass is defined.

Be very careful you don't get into an import loop.
That way lies madness.

John Roth

Jul 18 '05 #2

P: n/a
I tried that as I understand it. I'm sure I'm making a silly error in all
this. Here is a simple example of what I'm trying:

--- (File super_class.py) ---

class base_class:

def sample():
return "Returned!"

--- (File sub_class.py) ---

import super_class

class derived_class(base_class):

def sample2():
return "Returned Twice!"

--- (End) ---

Traceback (most recent call last):
File "/home/tina/python/test/sub_class.py", line 3, in -toplevel-
class derived_class(base_class):
NameError: name 'base_class' is not defined
John Roth wrote:

"Tina" <bad_addy@no_domain.com> wrote in message
news:E6***************@newsread2.news.pas.earthlin k.net...
In my program I need derived classes to inherit from the super class.

I can do this but I'd like to split the super class into it's own file.

How do you do this? When I split the code I start getting errors.


The module with the superclass has to be imported before
the subclass is defined.

Be very careful you don't get into an import loop.
That way lies madness.

John Roth


Jul 18 '05 #3

P: n/a

"Tina" <bad_addy@no_domain.com> wrote in message
news:kn***************@newsread1.news.pas.earthlin k.net...
I tried that as I understand it. I'm sure I'm making a silly error in all
this. Here is a simple example of what I'm trying:

--- (File super_class.py) ---

class base_class:

def sample():
return "Returned!"

--- (File sub_class.py) ---

import super_class

class derived_class(base_class):

def sample2():
return "Returned Twice!"

--- (End) ---

Traceback (most recent call last):
File "/home/tina/python/test/sub_class.py", line 3, in -toplevel-
class derived_class(base_class):
NameError: name 'base_class' is not defined
That needs to be

class derived_class(super_class.base_class):
pass

or else:

import base_class from super_class

HTH

John Roth


John Roth wrote:

"Tina" <bad_addy@no_domain.com> wrote in message
news:E6***************@newsread2.news.pas.earthlin k.net...
In my program I need derived classes to inherit from the super class.

I can do this but I'd like to split the super class into it's own file.

How do you do this? When I split the code I start getting errors.


The module with the superclass has to be imported before
the subclass is defined.

Be very careful you don't get into an import loop.
That way lies madness.

John Roth

Jul 18 '05 #4

P: n/a
Tina:
Instead of "import super_class", I think you need
"from super_class import base_class", or
"from super_class import *", or if you want to use "import super_class",
then define the derived class as
"class derived_class(super_class.base_class)"

Mike
Tina wrote:
I tried that as I understand it. I'm sure I'm making a silly error in all
this. Here is a simple example of what I'm trying:

--- (File super_class.py) ---

class base_class:

def sample():
return "Returned!"

--- (File sub_class.py) ---

import super_class

class derived_class(base_class):

def sample2():
return "Returned Twice!"

--- (End) ---

Traceback (most recent call last):
File "/home/tina/python/test/sub_class.py", line 3, in -toplevel-
class derived_class(base_class):
NameError: name 'base_class' is not defined
John Roth wrote:

"Tina" <bad_addy@no_domain.com> wrote in message
news:E6***************@newsread2.news.pas.earthl ink.net...
In my program I need derived classes to inherit from the super class.

I can do this but I'd like to split the super class into it's own file.

How do you do this? When I split the code I start getting errors.


The module with the superclass has to be imported before
the subclass is defined.

Be very careful you don't get into an import loop.
That way lies madness.

John Roth

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.