10  Classes

import sys
sys.version
'3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:41:22) [Clang 13.0.1 ]'
class Person:
    def __init__(self, FirstName, LastName, DOB, Height, Weight):
        self.FirstName = FirstName
        self.LastName = LastName
        self.DOB = DOB
        self.Height = Height
        self.Weight = Weight
p = Person("Julia", "Lang", (2012, 2, 12), 1.73, 51)
p
<__main__.Person at 0x106880430>
type(p)
__main__.Person
type(p.DOB)
tuple

10.1 Methods

Let’s add a method that calculates a person’s age in years today by substracting today’s date from their birthdate:

class Person:
    def __init__(self, FirstName, LastName, DOB, Height, Weight):
        self.FirstName = FirstName
        self.LastName = LastName
        self.DOB = DOB
        self.Height = Height
        self.Weight = Weight
    
    def getAge(self):
        from datetime import datetime
        return (datetime.now() - datetime(*self.DOB)).days/365
p = Person("Julia", "Lang", (2012, 2, 12), 1.73, 51)
p.getAge()
10.704109589041096

10.2 Resources