Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't get function within object to display value

hey guys I’m currently learning oop but I’m having a bit of trouble with accessing values which I create or add to a object, currently I can’t work out how to get the value stored within getAge

from datetime import date


class Person:
def __init__(self, name, dob, cob):
    self.name = name
    self.dob = dob
    self.cob = cob

def setName(self, name):
    self.name = name

def getAge(self):
    today = date.today()
    birthYear = int(self.dob[6:]) + 2000
    age = today.year - birthYear
    return age

  


p1 = Person('Jane', '22/06/02', 'England')

p1.setName("Joe")
print(p1.name)

print(Person.getAge)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I don’t understand why you print Person.getAge, it’s a method of Person.

getAge is a method and not static not, so you need use p1.getAge() istead of Person.getAge to get the age value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading