N
N
Nikita Beletsky2019-08-31 15:32:30
Python
Nikita Beletsky, 2019-08-31 15:32:30

Does my code follow the OOP paradigm?

I am learning Python, trying to understand the essence of programming as such, in particular, to master OOP. The code I provided works, you don’t have to delve too deeply into its essence (it just draws a triangle and a square of a user-specified height in the console). I'm interested in whether this creation corresponds to the OOP paradigm, and whether the principle of polymorphism is specifically implemented in this version. The definition of polymorphism I found here: "polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types) / polymorphism is the ability (in programming) to represent the same interface for different underlying forms (data types) )." I'm not very familiar with python syntax yet and I doubt that I correctly provided the same interface (class Drawer) for two data types (class Triangle, class Square), as bequeathed by this definition. If I made a mistake, please explain what would be more correct, using the OOP paradigm, to implement the functionality that my code provides, or show some of your own example.

class Drawer:
    def __init__(self, figure, elements, height):
        self.figure = figure
        self.elements = elements
        self.height = height

    def execute(self):
        if self.figure == "triangle":
            structure = [" " * (self.height - 1) + self.elements[0] + self.elements[2] + "\n"]
            string = 1
            padding = self.height - 1
            while string < self.height:
                if string + 1 == self.height:
                    structure.append(self.elements[0] + self.elements[1] * (self.height - 1) + self.elements[2])
                else:
                    structure.append(" " * (padding - 1) + self.elements[0] + " " * string + self.elements[2] + "\n")
                string += 1
                padding -= 1
            return print("".join(structure))
        elif self.figure == "square":
            structure = [self.elements[0] + self.elements[1] * (self.height + 1) + self.elements[0] + "\n"]
            string = 1
            while string < self.height:
                if string + 1 == self.height:
                    structure.append(self.elements[0] + self.elements[2] * (self.height + 1) + self.elements[0])
                else:
                    structure.append(self.elements[0] + " " * (self.height + 1) + self.elements[0] + "\n")
                string += 1
            return print("".join(structure))


class Triangle:
    def __init__(self, height=2):
        self.elements = ["/", "_", "|"]
        self.height = height

    def draw(self):
        draw = Drawer("triangle", self.elements, self.height)
        draw.execute()


class Square:
    def __init__(self, height=2):
        self.elements = ["|", "`", "."]
        self.height = height

    def draw(self):
        draw = Drawer("square", self.elements, self.height)
        draw.execute()


t1 = Triangle(10)
sq1 = Square(10)
t1.draw()
print()
sq1.draw()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
longclaps, 2019-08-31
@longclaps

Not at all.
I don't feel like redoing your code line by line, it's bad (that's ok, but the fact that it works is just fine). It should be something like this:

from abc import ABC, abstractmethod
from math import sin, cos, pi


class Figure(ABC):
    def __init__(self, height=2):
        assert 1 < height < 20  # незачем больше
        self.height = height

    @abstractmethod
    def draw(self):
        pass


class Triangle(Figure):
    def draw(self):
        for i in range(self.height - 1):
            print(f'{"/":>{self.height - i}}{"":>{i * 2}}\\')
        print('─' * (self.height * 2))


class Square(Figure):
    def draw(self):
        w = self.height * 2 - 2
        print('┌', '┐', sep='─' * w)
        for _ in range(self.height - 2):
            print('│', '│', sep=' ' * w)
        print('└', '┘', sep='─' * w)


class Ellipse(Figure):
    def __init__(self, height=7, width=None):
        assert 6 < height < 20  # меньше никак
        if width is None:
            width = height
        else:
            assert 6 < width < 20
        self.height, self.width = height, width

    def draw(self):
        buf = [[' '] * (self.width * 2) for _ in range(self.height)]
        rx, ry = self.width - 1., self.height / 2 - .5
        for i in range(100):
            φ = i * pi / 50
            buf[round(ry * (1 + sin(φ)))][round(rx * (1. + cos(φ)))] = '*'
        for row in buf:
            print(''.join(row))


Triangle(7).draw()
Square(7).draw()
Ellipse(7).draw()
Ellipse(17, 7).draw()

Look at the class hierarchy, do not worry about the implementation of methods.

S
stictt, 2019-09-02
@stictt

OOP as a paradigm requires strict heredity, it is not, then this is not OOP, no matter how embarrassing you are)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question