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?
-
pls correct the indentation..Avinash Raj– Avinash Raj2015年11月24日 09:49:04 +00:00Commented Nov 24, 2015 at 9:49
-
1Have you imported the base class in the CTC class ?Bridgekeeper– Bridgekeeper2015年11月24日 09:49:20 +00:00Commented Nov 24, 2015 at 9:49
-
You have syntax errors in your Salary class aswell, you are missing ' : ' and the indentation is wrongBridgekeeper– Bridgekeeper2015年11月24日 09:50:19 +00:00Commented Nov 24, 2015 at 9:50
-
1You forgot to put ":" after init and display methodsig-melnyk– ig-melnyk2015年11月24日 09:51:59 +00:00Commented Nov 24, 2015 at 9:51
-
3Are they in the same file?khelwood– khelwood2015年11月24日 09:52:42 +00:00Commented Nov 24, 2015 at 9:52
3 Answers 3
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`
3 Comments
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.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
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
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