4. Other examples of OOP¶
We can create a class
Person
with basic attributes.
20
import datetime
class Person:
def __init__(self, name, birthdate):
self.name = name
self.birthdate = birthdate
def age(self):
delta = datetime.date.today() - self.birthdate
return int(delta.days / 365)
def __str__(self):
return "%s, %d years old" % (self.name, self.age())
masanori = Person("Fernando Masanori", datetime.date(1980, 9, 1))
print(masanori.age())
print(masanori)
Activity: 4.1 ActiveCode (ac_l25_4a_en)
You can use the implementation of the class
Person
to create an object with your name.
2
# Create an object with your name
Activity: 4.2 ActiveCode (ac_l25_4b_en)
You have attempted 1 of 3 activities on this page