<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env python3
# -*- coding: utf-8 -*-

class RationalNumber:
    nInstances = 0 # class attribute, or static attribute
        
    def __init__(self, numerator, denominator):
       if not isinstance(numerator ,int) :
                 raise TypeError(
                            "numerator should be of type int ")
       if not isinstance(denominator ,int) :
                 raise TypeError(
                            "denominator should be of type int ")
       self.numerator=numerator
       self.denominator=denominator
       
       # increase counter
       self.inc()
       
    def __del__(self):
        self.dec()
           
    @classmethod
    def inc(cls):
        cls.nInstances += 1
        
    @classmethod
    def dec(cls):
        cls.nInstances -= 1
   
    @classmethod
    def status(cls):
        print(f"Currently {cls.nInstances} active!")
  
    @staticmethod
    def status2():
        print(f"Currently {RationalNumber.nInstances} active!")

    
    def convert2float(self):
        return float(self.numerator)/float(self.denominator)

    def add(self , other):
        p1 , q1 = self.numerator , self.denominator
        if isinstance(other , RationalNumber):
            p2 , q2 = other.numerator , other.denominator
        elif isinstance(other , int):
            p2 , q2 = other , 1
        else:
            raise TypeError("...")
        return RationalNumber(p1 * q2 + p2 * q1 , q1 * q2)

    def __add__(self, other):
        return self.add(other)

    def __radd__(self, other):
        if isinstance(other,int):
            return self.add(RationalNumber(other,1))
        elif isinstance(other, RationalNumber):
            return self.add(other)
        else:
            raise TypeError("Wrong input type")

    def __iadd__(self, other):
        new = self.add(other)
        self.numerator = new.numerator
        self.denominator = new.denominator
        return self

    def __repr__(self):
        return f"{self.numerator} / {self.denominator}"

    def shorten(self):
        def gcd(a, b):
            # Computes the greatest common divisor
            if b == 0:
               return a
            else:
               return gcd(b, a % b)
        factor = gcd(self.numerator , self.denominator)

        self.numerator = self.numerator // factor
        self.denominator = self.denominator // factor

################################################
##  main
################################################

q=RationalNumber(10,20)
print(q)
RationalNumber.status()
print(q.numerator)
print(q.denominator)

p = RationalNumber(24,35)

RationalNumber.status()

# q1 = q.add(p)

#print(f"Currently {RationalNumber.nInstances} instances of RationalNumber active")

del q 
RationalNumber.status()
RationalNumber.status2()
#print(f"Currently {RationalNumber.nInstances} instances of RationalNumber active")

</pre></body></html>