Lecture_4#_Objects_and_refences
Lecture_4#_Objects_and_refences
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
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.
__init__(self, name, power): A constructor method that initializes the name and power of the artifact.
__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:
__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")