#1
  1. Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    14
    Rep 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
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    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.
  4. #3
  5. Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    14
    Rep Power
    0
    Awesome! Obvious now you point it out. Many thanks.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12

    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!"
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    Good point ... never though of it that way.

    Cheers

IMN logo majestic logo threadwatch logo seochat tools logo