Some Special Methods in Python Class
Vera-778
Posted on March 25, 2024
class Square:
def init(self, side):
self.side = side
def eq(self, other):
return self.side == other.side
def ne(self, other):
return self.side != other.side
def lt(self, other):
return self.side < other.side
def le(self, other):
return self.side <= other.side
def gt(self, other):
return self.side > other.side
def ge(self, other):
return self.side >= other.side
sq1 = Square(10)
sq2 = Square(11)
print(sq1 != sq2)
print(sq1 < sq2)
print(sq1 <= sq2)
print(sq1 > sq2)
print(sq1 >= sq2)
+: object.add(self, other)
-: object.sub(self, other)
: object.mul(self, other)
/: object.div(self, other)
%: object.mod(self, other)
*: object.pow(self, other)
&: object.and(self, other)
^: object.xor(self, other)
|: object.or(self, other)
+=: object.iadd(self, other) ("i" for in place)
-=: object.isub(self, other)
=: object.imul(self, other)
/=: object.idiv(self, other)
%=: object.imod(self, other)
*=: object.ipow(self, other)
-: object.neg(self)
+: object.pos(self)
abs(): object.abs(self)
int(): object.int(self)
float(): object.float(self)
Posted on March 25, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024