Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 596609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:11:24+00:00 2026-05-13T16:11:24+00:00

I am trying to get completely to grips with class inheritence in Python. I

  • 0

I am trying to get completely to grips with class inheritence in Python. I have created program’s with classes but they are all in one file. I have also created scripts with multiple files containing just functions. I have started using class inheritence in scripts with multiple files and I am hitting problems. I have 2 basic scripts below and I am trying to get the second script to inherit values from the first script. The code is as follow’s:

First Script:

class test():

    def q():

        a = 20
        return a

    def w():
        b = 30
        return b

    if __name__ == '__main__':
        a = q()
        b = w()

if __name__ == '__main__':
    (a, b) = test()

Second Script:

from class1 import test

class test2(test):

    def e(a, b):
        print a
        print b


    e(a, b)

if __name__ == '__main__':
    test2(test)

Can anyone explain to me how to get the second file to inherit the first files values? Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:11:24+00:00Added an answer on May 13, 2026 at 4:11 pm

    I would say you messed up class definition with function stuff. It should look more like this:

    class Test(object):
    
        def __init__(self):
            self.a = 20
            self.b = 30
    
    if __name__ == '__main__':
        test_instance = Test()
    

    and

    from class1 import Test
    
    class Test2(Test):
    
        def e(self):
            print self.a
            print self.b
    
    
    if __name__ == '__main__':
        test_instance = Test2()
        test_instance.e() # prints 20 and 30
    

    It looks like your problem is not (only) inheritance, but also how to correctly define classes in Python.

    Some notes:

    • Always use capitalized names for classes. That is more or less convention.
    • As ruibm pointed out, every (non-static) method of a class has to have a first parameter that is named (by convention) self.
    • You can create instance variables by setting them as self.variable = value in the __init__ method.
    • If you call Test() you get an object back. Unless you assign it to a variable, just calling test2() as you did in your second piece of code has no effect. Maybe it had in your case because defined your class in a weird way.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get my feet wet in C, and I am curious
I am trying to get the knack of good system design. As there is
I have an entity structure as follows: IManager: IDeletable { IEnumerable<IFund> Funds {get;} IFailureNotification
I have a rails 2.3.8 app with rspec tests (not written by me, just
I'm completely self-taught, so there are obvious gaps in my knowledge. When I needed
I'm trying to load a video file into my iPad app as an AVURLAsset
I'm looking for an algorithm, and I have no idea where to start! I'm
I've been working on writing a script which automatically logs me into my school's
I've had MySQL running as a service on my Windows XP SP3 development desktop,
I've got an odd and persistent problem... (Also my right shift key just stopped

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.