Error message
C:\python\ipython\Tutorials\ Eduonix\exercises>C:\Users\xxxx\AppData\Local\P
rograms\Python\Python36-32\python man.py
Traceback (most recent call last):
File "man.py", line 31, in <module>
male = Male()
File "man.py", line 5, in __init__
say(self.type)
NameError: name 'say' is not defined
Source Code
class Human():
def __init__(self):
self.type = "human"
say(self.type)
def sleep(self):
pass
def eat(self,food):
print("I eat "+food)
def excrete(self):
pass
def say(self, type):
print("I am a "+type)
class Male(Human):
sex_chromosomes = "XY"
class Man(Male):
age = "elder"
def say(self):
say("male")
def say(self):
say("male")
male = Male()
Correction
The __init__ method is roughly what represents a constructor in Python and the self variable represents the instance of the object itself., If you want call the method within the class, you need to add the keyword self:
class Human():
def __init__(self):
self.type = "human"
self.say(self.type)
def sleep(self):
pass
def eat(self,food):
print("I eat "+food)
def excrete(self):
pass
def say(self, type):
print("I am a "+type)
class Male(Human):
sex_chromosomes = "XY"
class Man(Male):
age = "elder"
def say(self):
say("male")
def say(self):
say("male")
male = Male()
No comments :
Post a Comment