<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Bogdan]]></title><description><![CDATA[Bogdan]]></description><link>https://bogdans.hashnode.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1593680282896/kNC7E8IR4.png</url><title>Bogdan</title><link>https://bogdans.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 15 Sep 2026 17:30:32 GMT</lastBuildDate><atom:link href="https://bogdans.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[01 - OOP and Functional Programming]]></title><description><![CDATA[OOP - Object-Oriented Programming
OOP is the programming paradigm that revolves around using classes or otherwise custom objects to organise a codebase, by having all data regarding a certain entity, ]]></description><link>https://bogdans.hashnode.dev/01-oop-and-functional-programming</link><guid isPermaLink="true">https://bogdans.hashnode.dev/01-oop-and-functional-programming</guid><category><![CDATA[Python]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[Python Course]]></category><category><![CDATA[python programming]]></category><dc:creator><![CDATA[Bogdan]]></dc:creator><pubDate>Wed, 02 Sep 2026 16:05:50 GMT</pubDate><content:encoded><![CDATA[<h2>OOP - Object-Oriented Programming</h2>
<p>OOP is the programming paradigm that revolves around using <strong>classes</strong> or otherwise custom objects to organise a codebase, by having all data regarding a certain entity, along with functions to operate on or use that data, grouped within the same class/object.</p>
<p>OOP is often used because it can lead to cleaner codebases, and because it permits using abstraction, inheritance, encapsulation or polymorphism.</p>
<h2>FP - Functional Programming</h2>
<p>FP is the programming language paradigm that revolves around using <strong>functions</strong> to operate on data passed as arguments, rather than grouping data into a class/object.</p>
<p>FP is more rarely used because it's more limiting than OOP, but it also provides more readability at first sight, since it eliminates hidden behaviour of dunder methods.</p>
<h2>Examples</h2>
<p>An example calculator program could be:</p>
<h3>For FP</h3>
<pre><code class="language-python">def calculate(expression: str) -&gt; int:
    items = expression.split(" ")
    a, b = int(items[0]), int(items[2])
    operator = items[1]

    match operator:
        case '+':
            return a + b
        case '-':
            return a - b
        case '*':
            return a * b
        case '/':
            return a // b

x = calculate('2 + 3') # 5
x = calculate(f'{x} + 2') # 7
print(x) # 7
</code></pre>
<p>Notice how there is no persistent state from the function? That's the core of FP.</p>
<h3>For OOP</h3>
<pre><code class="language-python">from typing import Self

class Calculator:
    def __init__(self, value: int = 0) -&gt; None:
        self.value = value

    def calculate(self, expression: str) -&gt; Self:
        expression = expression.replace("x", str(self.value))
        items = expression.split(" ")
        a, b = int(items[0]), int(items[2])
        operator = items[1]
        
        match operator:
            case '+':
                self.value = a + b
            case '-':
                self.value = a - b
            case '*':
                self.value = a * b
            case '/':
                self.value = a // b

        return self

    def __str__(self):
        return str(self.value)

calc = Calculator()
calc.calculate('2 + 3')
calc.calculate('x + 2')
print(calc)  # 7

# Or:
calc = Calculator(2)
calc.calculate('x + 3').calculate('x + 2')
print(calc)
</code></pre>
<p>Notice how, with OOP, the class itself implements persistence rather than requiring the user to implement it themselves via variable assignments? That gets more useful when you need multiple values.</p>
<h2>Summary</h2>
<p>Functional Programming lets you understand what everything does quicker at first sight, at the cost of requiring you to store data in variables and pass it through parameters.</p>
<p>Object-Oriented Programming lets you organise data and functions within one class/object, reducing the burden of implementation from the user, at the cost of hidden behaviour that might not be expected at first sight (such as <code>x</code> being replaced with the stored value, in the example above).</p>
]]></content:encoded></item></channel></rss>