PYW.png 9b: Object Oriented Programming #2

Table of Contents

1. Questions from Tuesday

  1. Create a function that shows the content of a file, asks or user input, and adds the user input to the file.
def file_opener():
    file_name = "/home/misha/Nextcloud/scripts_misha/klad/example-file.txt"

    with open(file_name, "r") as f:
        var1 = f.read()

    print(var1)

    user_input = input("Add something to the file: ")

    with open(file_name, "a") as f2:
        f2.write("\n\n\n\n")
        #f2.write(f"\n{user_input}") #also possible
        f2.write(user_input)


    with open(file_name, "r") as f:
        var1 = f.read()
    print(var1)

file_opener()        
  1. Vertically flip the "christmass tree" written to a text file above.
  2. Create a list comprehension that stores a list of integers as a list of strings.
  3. Create a class for rectangles. Create two instances each with different values for the attributes "width" and "height".
lst = [i for i in range(5)]

print(lst)

2. OOB continued

2.1. Classes and objects

An instance of a class is called an object.

By convention we use CamelCase for defining the name of a class.

# A class definition must contain at least one statement. In this case, the
# description is used as such.
class Point:
    """Represents a point in 2-D space."""

p1 = Point()    

How to add attributes to this instance?

class Point:
    """Represents a point in 2-D space."""

p1 = Point()

p1.x = 2.0
p1.y = 8587

2.2. Methods

A method is a function that is defined within a class and is used to perform actions or operations on instances of that class.

The first argument in the definition of the method is always "self", and you use this "self" - in the definition of the method - when assigning values to attributes, or for calling attributes.

But when calling (running) the method you do not have to add it as an argument.

class Point:
    def move_right(self, i):
        self.x = self.x + i

p1 = Point()
p1.y = 4

p1.move_right(10)

print(p1.x)

2.3. Initialization

There is a special method that ensures that new instances of a class always have certain predefined attributes, with predefined values.

If the values are predefined (always the same), the `_init__` method only takes one argument: self.

class Point:
    def __init__(self):
        self.x = 0
        self.y = 4

p1 = Point()
print(p1.y)

It is possible to set the `_init__` function up in such a way that you can define the values of the initial attributes when creating an instance of the class:

class Point:
    def __init__(self, xcoordinate, ycoordinate):
        self.x = xcoordinate
        self.y = ycoordinate

p1 = Point(2,2)
print(p1.y)

2.4. How to represent an object

class Rectangle():

    def __init__(self, length, width):
        self.length = length
        self.width = width

    def perimeter(self):
        return (2*self.length) + (2*self.width)

    def __repr__(self):
        return f"Length: {self.length}\nWidth: {self.width}"

r1 = Rectangle(1,2)

print(repr(r1))

Created: 2025-04-11 vr 15:17