SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old June 11th, 2003, 05:10 AM
HarryF's Avatar
HarryF HarryF is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 14 HarryF User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old June 11th, 2003, 05:42 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #3  
Old June 11th, 2003, 06:15 AM
HarryF's Avatar
HarryF HarryF is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 14 HarryF User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Awesome! Obvious now you point it out. Many thanks.

Reply With Quote
  #4  
Old June 12th, 2003, 08:55 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
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!"

Reply With Quote
  #5  
Old June 14th, 2003, 10:40 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #6  
Old June 14th, 2003, 11:04 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
Good point ... never though of it that way.

Cheers

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Import and Inheritance Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway