Documente Academic
Documente Profesional
Documente Cultură
a.y. 2018-2019
Lesson 14
Classes, attributes, methods
SEDIN
IT Education Services Center
2
Objects
• Python supports different types of data:
1946 3.1415926535 "Hello World!" [1, 5, 7, 11]
name
Person
last_name John
Snow
Classes of objects
• Before creating an object it is necessary to define it
• Assuming to be able to create an object through a «mold», the definition
of the object represents the creation of the mold
Creation of a class
• To create a class you need to give it a name and define its attributes
and/or methods
• All instances of the class inherit attributes and methods of the class to
which they belong
class Person():
''' Class of objects of type: Person '''
attribute
attribute
attribute
attributes of the class
def method(self):
instruction
instruction
method of the class
8
Attributes of a class
• They allow to customize each single object of the class
• They can be Python built-in types (i.e.: int, float, list) or other objects
• They can be defined directly in the class (with an assignment, as shown
here below), but they are usually defined with a special method called
constructor method
class Person():
name = "" # str, initially null
last_name = "" # str, initially null
height = 0 # int, cm, initially zero
weight = 0.0 # float, kg, initially zero
9
required parameter
self parameter optional parameters
class Person():
esteban = Person('mexican')
juliette = Person('french')
12
Copy of objects
mary mary2
• A simple assignment cannot be used to copy objects
• mary2 = mary .name 'Mary'
- it will not create a copy of mary in mary2 .last_name 'White'
- it will define a second variable, mary2, addressing the same memory area .height 183
of mary, then to the same object .weight 70.6
• mary3 = copy.copy(mary)
- uses the copy() function of the copy library
.name 'Mary'
- the library must first be imported (import copy)
.last_name 'White'
- performs a copy of the object in memory (mary) in another memory area,
.height 183
and assigns the memory address to access the object to a new variable
(mary3) .weight 70.6
14
Methods
• They are not variables/objects that contain a PERSON
.name
value, but procedures that execute instructions
.last_name
.height
• They can return a value thanks to the return .weight
statement .BMI ()
Instance_Name.Method_Name(parameters)
15
def BMI(self):
bmi_value = self.weight / (self.height/100)**2
return bmi_value
keyword to define keyword to allow a formula for the calculation of the body mass
a method method to return a value index, which will be returned by the method
16
def __str__(self):
t = (self.name + " " +
self.last_name + " (" + .name "Mary"
str(self.height) + "cm, " + .last_name "White"
str(self.weight) + .height 183
"kg, has a Body Mass Index of " +
.weight 70.6
str(round(self.BMI(), 1)) + ')')
return t .nationality 'english'
>>> print(mary)
Mary White (183cm, 70.6kg, has a Body Mass Index of 21.1)
17
class Person():
pass
class Student(Person): subclass of the objects category
pass
class Professor(Person):
pass superclass for Student and Professor
subclasses of Person
18
Access to files
To open a file in Python we use the open function, that creates a file object
to which is associated the specific file we want to open.
In Python the built-in function to access files is open()
• it opens a file and returns an file
directory
file
(text)
file
(binary)
Operating System
a variable to which the object
should be associated
19
Reading methods
The content of a file can be read entirely or line by line:
f = open('students.txt')
row = f.readline()
while row != '':
print(row)
row = f.readline()
f.close()
21
f.write("Nina Ferdi\n")
f.close()
to end a line in the text file, the
"new line" character is needed
f = open("students.txt", "a")
f.write("Lola Manno\n") opens the file in add or append mode. Python
positions itself at the end of the last line of the
file, therefore all the data Python writes in the
f.close() file is added to the end of the document
22
Book references
Learning Python:
Chapters 12, 10.1, 10.2, 10.3
Assignment:
Exercises of lesson 14