0

Am new to Python and was exploring Classes and Object. I have created a class,defined few function in it. Then I have created another class and was trying to inherit from the first class but got some error. Error: class CTC(Salary): NameError: name 'Salary' is not defined

Base Class:

class Salary:

    monthly=0.00
    name = ""

    def __init__(self,name,monthly):
        self.name = name
        self.monthly = monthly


    def display(self):
        print("name: ", self.name,  "Monthly Salary: ", self.monthly)

Derived Class:

class CTC(Salary):

    tax=0.00
    ctc=0.00
    def __init__(self,name,monthly,tax):
        Salary.__init__(self,name,monthly)
        self.tax = tax


    def calculateCTC(self):
         yearly = monthly*12
         totalTax= tax *12
         ctc = yearly - totalTax
         print("Total CTC: ", self.ctc)

         obj = CTC("Rishi",28700.00,1295.00)
         obj.display(self)

Can anyone explain me the root cause for the error?

18
  • pls correct the indentation.. Commented Nov 24, 2015 at 9:49
  • 1
    Have you imported the base class in the CTC class ? Commented Nov 24, 2015 at 9:49
  • You have syntax errors in your Salary class aswell, you are missing ' : ' and the indentation is wrong Commented Nov 24, 2015 at 9:50
  • 1
    You forgot to put ":" after init and display methods Commented Nov 24, 2015 at 9:51
  • 3
    Are they in the same file? Commented Nov 24, 2015 at 9:52

3 Answers 3

2

I put all code in one file (with minor modifiactions) and it works form me.

class Salary:

    def __init__(self, name, monthly):
        self.name = name
        self.monthly = monthly

    def display(self):
        print("name: ", self.name,  "Monthly Salary: ", self.monthly)


class CTC(Salary):

    def __init__(self, name, monthly, tax):
        Salary.__init__(self, name, monthly)
        self.tax = tax
        self.ctc = 0.00 # create with default value

    def calculateCTC(self):
        yearly = self.monthly*12 # with `self`
        totalTax = self.tax*12 # with `self`
        self.ctc = yearly - totalTax # with `self`
        print("Total CTC: ", self.ctc)

# without indentation
obj = CTC("Rishi", 28700.00, 1295.00)
obj.display() # without `self`

if you need it in separated files

salary.py

class Salary:

    def __init__(self, name, monthly):
        self.name = name
        self.monthly = monthly

    def display(self):
        print("name: ", self.name,  "Monthly Salary: ", self.monthly)

main.py

from salary import Salary

class CTC(Salary):

    def __init__(self, name, monthly, tax):
        Salary.__init__(self, name, monthly)
        self.tax = tax
        self.ctc = 0.00

    def calculateCTC(self):
        yearly = self.monthly*12 # with `self`
        totalTax = self.tax*12 # with `self`
        self.ctc = yearly - totalTax # with `self`
        print("Total CTC: ", self.ctc)

# without indentation
obj = CTC("Rishi", 28700.00, 1295.00)
obj.display() # without `self`
Sign up to request clarification or add additional context in comments.

3 Comments

Hi furas it worked. But still I could not understand where I was missing
Also, you have removed all the global variables. If I am trying to call calculateCTC after declaring global variables its throwing same error again
@RISHIKHANNA Actually, he has not removed the global variables, when defining a variable with self it will be in a global scope to that class, and any class derived from that class. when you define a variable without the self keyword, the variable will only be in a local scope.
1

I formated your code and it works for me.

class Salary:

    monthly=0.00
    name = ""

    def __init__(self,name,monthly):
        self.name = name
        self.monthly = monthly


    def display(self):
        print("name: ", self.name,  "Monthly Salary: ", self.monthly)


class CTC(Salary):

     tax=0.00
     ctc=0.00
     def __init__(self,name,monthly,tax):
         Salary.__init__(self,name,monthly)
         self.tax = tax


     def calculateCTC(self):
         yearly = monthly*12
         totalTax= tax *12
         ctc = yearly - totalTax
         print("Total CTC: ", self.ctc)

         obj = CTC("Rishi",28700.00,1295.00)
         obj.display(self)

3 Comments

Are you using same file to embed both the classes? As I m using different files
@RISHI KHANNA I my case i use the same file, if you like i will use two file for demostration.
Got it finally. Thanks for the help :)
-1

Unlike Java, which forces developer to put each class in each file, Python is more flexible. In Python you can write as much code as you want in a single file, meaning that you can have both of your classes in a single file, without needing to import anything. The diffrence is in importing. Java doesn't need you to import any of your project files, Python on the other hand requires you to import anything that is in external packages, no matter where are they kept. So just import your Salary to the file with CTC.

2 Comments

Yes. just wanted to try different way. Imported Salary to CTC but still no luck
Did you just import salary? if so you need to refer to your class with salary.Salary. You can however directly import the class with from salary import Salary. I assume that your file is named salary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.