|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
||||
|
||||
|
Import and Inheritance Problem
I've got two classes in seperate files, the Goodbye class being a subclass of Hello;
Code:
#Hello.py
class Hello:
def __init__(self):
self.msg = "Hello World!"
def getMsg(self):
return self.msg
Code:
#Goodbye.py
import Hello
class Goodbye(Hello):
def __init__(self):
self.msg = "Goodbye World!"
Now trying to use these classes in another script; Code:
import Hello import Goodbye hello = Hello.Hello() print hello.getMsg() goodbye = Goodbye.Goodbye() print goodbye.getMsg() The first call print hello.getMsg() works fine, displaying "Hello World!" Instantiating the Goodbye class results in a type error; "Module "object" is not callable" Hope someone can put me right here. Many thanks |
|
#2
|
||||
|
||||
|
Subclass
Hi HarryF,
Solved your problem, You missed out the Hello. when you subclassed Hello.Hello() , easily done. Goodbye.py should read -Code:
#Goodbye.py
import Hello
class Goodbye(Hello.Hello):
def __init__(self):
self.msg = "Goodbye World!"
Hope this helps, Mark. |
|
#3
|
||||
|
||||
|
Awesome! Obvious now you point it out. Many thanks.
|
|
#4
|
|||
|
|||
|
Re: Subclass
or you could just use
from Hello imprt * Code:
#Goodbye.py
from Hello imprt *
class Goodbye(Hello):
def __init__(self):
self.msg = "Goodbye World!"
|
|
#5
|
||||
|
||||
|
from Hello import but this really isn't a good coding practive when it comes to calling objects. It's just a personal preferance but I only ever use import, it helps to see where your calling from if you ask me.
Mark. |
|
#6
|
|||
|
|||
|
Good point ... never though of it that way.
Cheers |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Import and Inheritance Problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|