Modules -> OOP -> Classes and objects -> Composition

Composition


Related code
class Family:
    """Represents a family."""

class Person:
    """Represents a person."""

We can compose class instances and their attributes with any other construct or data structure.

Value of an attribute can be a list:

...
f = Family()
f.name = "Krasniqi"
f.members = ["Ali", "Bastrie"]

Class instances can be list items:

...
p1, p2 = Person(), Person()
p1.name, p2.name = "Ali", "Bastrie"

f.members = [p1, p2]

f.members is a list, to which all list operations and methods can be applied:

...
p3 = Person()
p3.name = "Amire"
f.members.append(p3)
>>> f.members
[<Person object at 0x0000015E227D8CD0>, <Person object at 0x0000015E22F49010>, <Person object at 0x0000015E22F4A550>]
>>> f.members[1].name
Bastrie

Here is an example of a dictionary where its values are Person instances:

...
>>> members = {'p1': Person(), 'p2': Person()}
>>> members['p1'].name = "Maksut"
>>> members['p2'].name = "Maksute"
>>> p1 = members['p1']
>>> p1.name
Maksut

Exercises


Create two classes, Company and Employee.


Create some employees and assign their names.


Create two companies, add an employees: set attribute and add some employees to each of the companies.


Check if there's any employee working in both companies and print them.




Create two classes, Library and Book.


Create a library, library and add some books to library.books dictionary whose keys are characters and values are list of books whose title start with that character. Example:

{'a': [<Book whose title is 'Ali baba'>, ...], 'o': [<Book whose title is 'Of Mice and Men'>, ...]}


Add a new book that starts with 's'.


Print the first book that starts with 's'.




Create two classes, Flight and Passenger.


Create three passengers and assign a name to each.
Create a flight and add some passengers to its passengers: list attribute.


Remove the last passenger from the flight


Print number of the flight passengers.




Create two classes, Market and Product.


Create four products and assign a name and a price to each.
Create two markets and add some products to their products: list attribute.


Print number of products of the first market.


Print total product value of the second market.



class Hospital:
    pass

class Doctor:
    pass

d1, d2 = Doctor(), Doctor()
d1.name, d2.name = "Dr. Liri", "Dr. Mirlinda"

h = Hospital()
h.staff = {"cardiology": d1, "urologji": d2}

print(h.staff["urologji"].name)

What is printed when we execute the above script




Exercises


Create two classes, School and Student.


Create a school and add some students to its students: list attribute.


Add one more student to school.




Create two classes, Recipe and Ingredient.


Create some ingredients with one attribute, name.
Create a recipe and add created ingredients to its ingredients: tuple attribute.


Print the name of the first ingredient of the created recipe.




Create two classes, Zoo and Animal.


Create some animals with one attribute, name.
Create a zoo and add created animals to its animals: set attribute.


Add one more animal to the zoo.




Create two classes, Concert and Performer.


Create some performers with one attribute, name.
Create a concert and add created performers to its performers: list attribute.


Print names of all concert performers



class Bakery:
    pass

class Cake:
    pass

c1, c2 = Cake(), Cake()
c1.name, c2.name = "Chocolate", "Vanilla"

b = Bakery()
b.cakes = [c1, c2]

c = b.cakes.pop(-1)
print(c.name)

What is printed when we execute the above script