class Human:
def __init__(self,
name):
self.person=name
self.head=1
self.eye=2
self.leg=2
self.hand=2
self.mouth=1
self.nose=1
self.body=1
def sing(self):
print(f"{self.person} have {self.head} head")
print(f"{self.person} have {self.eye} eyes")
print(f"{self.person} have {self.mouth} mouth")
print(f"{self.leg} legs {self.hand} hands")
print(f"{self.body} body body body {self.nose} nose")
human_object=Human("Joseph")
human_object.sing()
EXPLANATION
Line 1: Create a class Called Human
Line 2: Create an __init__ constructor Method which is called when the instance of the class Human is created. The __init__ constructor initialize the value of the object.
Line 3-10: Assign the parameter (name) and value (1 or 2) to the attribute person, eye, head etc.
Line 11: Declare a method called sing that print a Nigerian Pre-nursery song. The sing method define the behaviour of the method. The sing
method uses self
to refer to the current instance's attributes (like self.head
, self.eye
, etc.)
Line 17: Creates the object or instance of the class
Line 18: Calls the sing method of the human class.
TRY IT YOURSELF
Write an run the code on your text editor and change some parameter or values, Re run the program to see the result.
CONCLUSION
The tutorial provide a concise guide on how to create class with python. It offers syntax, code example and explanation on the code.