Python Magic Methods

Python Magic Methods

Instructor-svgAl-Mamun Sarkar
Mar 25 , 2020

Python Magic Methods. The following code shows how to use __init__, __del__, __gt__, __lt__, __ge__, __le__ in the python programming language.

Code:

class MyClass:

    def __init__(self, var):
        self.var = var
        print ("Constructor")

    def __del__(self):
        del self.var
        print ("Destructor")

    def __gt__(self, other):
        return len(other) > len(self.var)

    def __lt__(self, other):
        return len(other) < len(self.var)

    def __ge__(self, other):
        return len(other) >= len(self.var)

    def __le__(self, other):
        return len(other) <= len(self.var)


obj = MyClass(5)

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram