

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


class Name: def init(self): self.firstname = "[no first name]" self.lastname = "[no last name]"
class Person:
def init(self):
self.name = Name() self.eyecolor = "[no eye color]" self.age = - 1
A couple of things worth noting as you study this:
Name is a class used as an attribute inside Person — this is called composition , where one object contains another. self.name = Name() on line 12 creates a new Name instance as part of every Person object, so each person has their own firstname and lastname. Default values like "[no first name]" and -1 for age are placeholders, typically overridden when you create specific instances.
class Person:
def init(self):
self.firstname = "[no first name]" self.lastname = "[no last name]" self.eyecolor = "[no eye color]" self.age = - 1
myPerson = Person()
print(myPerson.firstname) print(myPerson.lastname) print(myPerson.eyecolor) print(myPerson.age)
class Name: def init(self): self.firstname = "[no first name]" self.lastname = "[no last name]"
class Person: def init(self): self.name = Name() self.eyecolor = "[no eye color]" self.age = - 1
myPerson = Person()