0% found this document useful (0 votes)
2 views

Lecture_4#_Objects_and_refences

The document covers key concepts of Object-Oriented Programming (OOP) in Python, including aggregation, composition, and the use of objects as arguments in functions and methods. It explains the differences between aggregation and composition, emphasizing the independence of contained objects in aggregation. Additionally, it provides an assignment to create classes for a secret society, detailing the attributes and methods required for the Artifact, Member, and Society classes.

Uploaded by

Dong Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture_4#_Objects_and_refences

The document covers key concepts of Object-Oriented Programming (OOP) in Python, including aggregation, composition, and the use of objects as arguments in functions and methods. It explains the differences between aggregation and composition, emphasizing the independence of contained objects in aggregation. Additionally, it provides an assignment to create classes for a secret society, detailing the attributes and methods required for the Artifact, Member, and Society classes.

Uploaded by

Dong Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Object Oriented Programming

Using
Python

Teemu Matilainen
teemu.matilainen@savo
nia.fi
Lecture 4#

• Aggregation
• Composition
• Objects and references
• Objects as arguments to functions
• Objects as arguments to methods
• Instance of the class as an argument to a method
Lecture 4#

Aggregation

Aggregation is a concept in object-oriented programming (OOP) that represents a


"has-a" relationship between two classes. It is a form of association where one class
contains an object of another class but does not control the lifecycle of that object.
In simpler terms, it denotes a relationship where one class is part of another class.
Aggregation

In aggregation, the contained


object can exist independently of
the containing object. It doesn't
imply ownership, and the contained
object can be shared among
multiple instances of the containing
class or even exist outside of it.
Aggregation (Car)

Aggregation is different from


composition, where the contained
object is considered a part of the
containing object, and its lifecycle is
controlled by the containing object. In
aggregation, the contained object is
more independent and can be shared
among multiple objects.
Composition:

When using Composition,


the difference is that here
we are not creating an
object of the Engine inside
the Car class, rather than
that we are creating an
object of the Engine outside
and passing it as a
parameter of Engine class
which yields the same
result.
Car 1 Car 2

Cars
Objects and
references
Let’s create
a class
Engine in
its own file
Every value in Python is
an object. Any object you
create based on a class
you've defined yourself
works exactly the same as
any "regular" Python
object. For example,
objects can be stored in a
list:
The references at indexes 0, 3
and 4 in the list refer to the same
object. Either one of the
references can be used to access
the object. The reference at index
1 and 2 refers to a different object,
although with seemingly the same
contents. Changing the contents
of this latter object does not affect
the other one.

Reference
Dictionary
usage for
Objects:
• Let us used dictionary
to store the object
data structure.
How can we test
references?
The operator is been used for
checking if the two references refer to
the exact same object, while the
operator == will tell you if the contents
of the objects are the same.
Objects as arguments to functions
Let's have a look at a simple
example where a function receives
a reference to an object of type
my_first_car as its argument. The
function then changes the name of
the car. Both the function and the
main function calling it access the
same object, so the change is
apparent in the main function as
well.
Objects within
function

It is also possible to
create objects within
functions. If a function
returns a reference to the
newly created object, it is
also accessible within the
main function!!!!!
Objects as arguments to methods
Objects can act as
arguments to
methods.

Car Class contains a


is_the_same method
what compares car
object and another car
object engines (given as
an argument)
Instance of the class as an
argument to a method
Let's assume we
want to write a
program which
compares car
objects. We can
write a separate
function for this
purpose
One of the principles of
object-oriented
programming is to include
any functionality which
handles objects of a
certain type in the class
definition, as methods. So
instead of a function we
could write a method
which allows us to
compare the car with
another car object

Here the car object which the


method is called on is referred to as
self, while the other car object is c1.

Remember, calling a method differs


from calling a function. A method is
attached to an object with the quote
notation: “Car”
Is there
any other
way to
compare
objects?
Assignment 6#
Secret Society with Artifacts
Create a class named Artifact with the following attributes:

name (string): the name of the artifact.


power (string): the power or ability associated with the artifact.

Implement methods within the Artifact class:

__init__(self, name, power): A constructor method that initializes the name and power of the artifact.

Create a class named Member with the following attributes:

secret_identity (string): a unique identifier for the member.


name (string): the public name of the member.
access_level (string): the level of access the member has within the society.
artifacts (list): a list to store instances of the Artifact class representing artifacts owned by the member.

Implement methods within the Member class:

__init__(self, secret_identity, name, access_level): A constructor method that initializes the secret_identity, name, and
access_level of the member.

add_artifact(self, artifact): A method that takes an Artifact object as an argument and adds it to the member's list of
artifacts.
Create a class named Society with the following attributes:

society_name (string): the name of the secret society.


members (list): a list to store instances of the Member class representing society members.

Implement methods within the Society class:

__init__(self, society_name): A constructor method that initializes the society_name and an empty list for members.

add_member(self, member): A method that takes a Member object as an argument and adds the member to the
society.

remove_member(self, member): A method that takes a Member object as an argument and removes the member
from the society.

print_society_info(self): A method that prints information about the society, including the society name, a list of
members, and their associated artifacts.
Create instances of the
Artifact, Member, and Society
classes, add members to the
society, associate artifacts with
members, and print
information for both members
and the society
You can copy the test code from
here:
# Create instances of the Artifact, Member,
and Society classes
artifact1 = Artifact("Invisibility Cloak",
"Camouflage")
artifact2 = Artifact("Mind Control Amulet",
"Persuasion")

member1 = Member("S001", "Agent Smith",


"High")
member2 = Member("S002", "Agent Brown",
"Medium")

# Add artifacts to members


member1.add_artifact(artifact1)
member2.add_artifact(artifact2)

# Create an instance of the Society class


secret_society = Society("The Illuminati")

# Add members to the society


secret_society.add_member(member1)
secret_society.add_member(member2)

# Print information for each member and the


society
secret_society.print_society_info()

You might also like