====== Computer Lab 10, objects and classes I ======
A circle is a 2D geometry shape characterized by the position of its centre and a radius. How can it be represented in Python?
- Store the centre (//x// and //y// coordinates) and radius (//r//) as separate variables.
- Store //x//, //y// and //r// as elements in a list or a tuple.
- Create a new type to represent circles as objects.
The last option is the prefered one. It might be more difficult to implement a class/object but offers much more additional possibilities.
* A class starts with a ''class'' keyword:
class Circle:
"""represents a circle in 2D space"""
* A construction usually initializes object's data. Objects/class data are called **attributes**.
def __init__(self, x, y, r):
self.x = x
self.y = y
self.r = r
* Now, let's create 2 circles objects:
if __name__ == "__main__":
c1 = Circle(1, 2, 2)
c2 = Circle(4, 6, 4)
print(c1.x, c1.y, c1.r)
print(c2.x, c2.y, c2.r)
* Object methods operate on the object. Object/class functions are called **methods**.
class Circle:
(...)
def get_area(self):
area = 3.14 * (self.r ** 2)
return area
if __name__ == "__main__":
c1 = Circle(1, 2, 2)
print(c1.get_area())
* Create a function (in global scope) which takes 2 circles as arguments and returns True/False if these two circles intersect.
def is_intersect(c1, c2):
"""Tells if the two circles intersect or not.
:param c1: Circle object
:param c2: Circle object
:return: boolean: True if the c1, c2 intersect, otherwise False
"""
if __name__ == "__main__":
c1 = Circle(1, 2, 2)
c2 = Circle(4, 6, 4)
print(is_intersect(c1,c2))
* Usually, it is better to include all the functions operating on the objects directly in the corresponding class (thus making them methods):
class Circle:
(...)
def insersects_with(self, other):
"""Tells if this and other circles intersect or not.
:param other: Circle object
:return: boolean: True if self and other intersect, otherwise False
"""
if __name__ == "__main__":
c1 = Circle(1, 2, 2)
c2 = Circle(4, 6, 4)
print(c1.insersects_with(c2))
* Next, create a method accepting 2 parameters: //dx// and //dy// that define a shift of the circle's centre. This method should return a copy of itself with a shifted centre.
class Circle:
(...)
def get_shifted_copy(self, dx, dy):
if __name__ == "__main__":
c1 = Circle(1, 2, 2)
print(c1.x, c1.y, c1.r)
c1_shifted = c1.get_shifted_copy(5, -4)
print(c1.x, c1.y, c1.r)
print(c1_shifted.x, c1_shifted.y, c1_shifted.r)
1 2 2
1 2 2
6 -2 2
* Let's try to use our object in a print() function:
if __name__ == "__main__":
c1 = Circle(1, 2, 2)
print(c1)
<__main__.Circle object at 0x0000029E0C649E50>
* Well, we don't really care in which address in the memory the //c1// object is located. Let's implement the ''%%__str__(self)%%'' function to overwrite this behaviour:
class Circle:
(...)
def __str__(self):
return "Circle: x={}, y={}, r={}".format(self.x, self.y, self.r)
if __name__ == "__main__":
c1 = Circle(1, 2, 2)
c2 = Circle(-5, 0, 6)
print(c1)
print(c2)
Circle: x=1, y=2, r=2
Circle: x=-5, y=0, r=6
=== Weekly Homework 10 - Book object ===
Create a class representing a book (catalog card in a library). Your class must have the following structure. Copy-paste the following code and implement the methods:
Required filename: ''10_weekly_hw.py''.
class Book:
"""represents a book"""
def __init__(self, title, author, year):
"""
:param title: string, book title
:param author: string, book author
:param year: int, year of publication
"""
def __str__(self):
"""
>>> book1 = Book("I, Robot", "Isaac Asimov", 1950)
>>> print(book1)
Book: I, Robot by Isaac Asimov (1950)
>>> book2 = Book("Pride and Prejudice", "Jane Austen", 1813)
>>> print(book2)
Book: Pride and Prejudice by Jane Austen (1813)
"""
def has_same_author(self, other):
"""Compares authors of this and the other book.
:param other: Book object
:return: boolean, True if self and other have the same author, otherwise False
>>> book1 = Book("I, Robot", "Isaac Asimov", 1950)
>>> book2 = Book("Pride and Prejudice", "Jane Austen", 1813)
>>> book3 = Book("Sense and Sensibility", "Jane Austen", 1811)
>>> book1.has_same_author(book3)
False
>>> book2.has_same_author(book3)
True
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
===== homework =====
Solve homework [[courses:be5b33prg:homework:regular:05_fraction_class|05 - Fraction class]] and submit it via Upload system in time! Check the deadline in Upload system.
Solve homework [[courses:be5b33prg:homework:regular:06_prisoners_dilemma:start|06 - Prisoner's dilemma]] (simple player) and submit it via Upload system in time! Check the deadline in Upload system.