May-08-2021, 06:15 PM
Hello all
I am slowly moving ahead with my python journey. I wanted to ask the following regarding Object Oritentation.
Question 1)
I have created a class called human which when instantiated requires the attributes sex, eye color, height.
I have created 2 x methods one which calculates the age and one method that just prints all the objects attributes to the screen.
Thank you.
I am slowly moving ahead with my python journey. I wanted to ask the following regarding Object Oritentation.
Question 1)
I have created a class called human which when instantiated requires the attributes sex, eye color, height.
I have created 2 x methods one which calculates the age and one method that just prints all the objects attributes to the screen.
from datetime import date class human(): def __init__(self, sex, eyecolor, height, yearborn): self.sex = sex self.eyecolor = eyecolor self.height = height self.yearborn = yearborn print(self.sex, self.eyecolor, self.height, self.yearborn) def age(self): currentdate = date.today() a = currentdate.year - self.yearborn return a def display(self): print(self.sex) print(self.eyecolor) print(self.height) print(self.yearborn)When I create an object I use the following code:-
import humanclass Tom = humanclass.human("Male","Green", 173, 1994)I get the following output:-
Male Green 173 1994My question is this, is there anyway I can include the name of the object in the def __init__ function so that when python prints the attributes out using the code:-
print(self.sex, self.eyecolor, self.height, self.yearborn)whatever the user called the object name is also printed out in this case Tom?
Thank you.