Please refer to my previous article about classes if you don't have any prior knowledge about class init function or just classes in general.
Sample Code
This Class is about vectors. Please take a moment to read the init function, which initializes the condition and unpacks the input by using *args.
class Vector():
'''
Create a vector, input type = Vector(x, y)
'''
def __init__(self, *args)
if len(args) <= 1:
self._x, self._y = 0
else:
self._x, self._y = args
The text between '''s are information about a particular function: you can check them by using doc. In this case, in order to print 'Create a vector, input type = Vector(x, y)', you simply need to write:
print(Vector.__init__.__doc__)
repr
class Vector():
'''
Create a vector, input type = Vector(x, y)
'''
def __init__(self, *args)
if len(args) <= 1:
self._x, self._y = 0
else:
self._x, self._y = args
def __repr__(self):
'''
returns the information of Vector in form Vector(x, y)
'''
return 'Vector(%r, %r)' % (self._x, self._y)
# Call
v1 = Vector(2, 8)
print(v1) # prints Vector(2, 8) -- called __repr__ function
Built-in functions can be called easily with shortcuts (most of them) and repr is one of the easiest functions to activate. Just print(instance). printing an instance will activate the repr function and present the data in the form you want.
add
class Vector():
'''
Create a vector, input type = Vector(x, y)
'''
def __init__(self, *args)
if len(args) <= 1:
self._x, self._y = 0
else:
self._x, self._y = args
def __repr__(self):
'''
returns the information of Vector in form Vector(x, y)
'''
return 'Vector(%r, %r)' % (self._x, self._y)
def __add__(self, other):
'''
addition
'''
return Vector(self._x + other._x, self._y + other._y)
# Call
print(Vector.__add__.__doc__) # prints 'addition'
v1 = Vector(8, 2)
v2 = Vector(2, 4)
# Calling the __add__ function
print(v1 + v2) # prints 'Vector(10, 6)'
add is a built-in function, which means that you'll be able to simply activate it by using the + sign. if you need to add specific data from each instance, or to extract certain data and add them up together, add is the function to use.
sub
class Vector():
'''
Create a vector, input type = Vector(x, y)
'''
def __init__(self, *args)
if len(args) <= 1:
self._x, self._y = 0
else:
self._x, self._y = args
def __repr__(self):
'''
returns the information of Vector in form Vector(x, y)
'''
return 'Vector(%r, %r)' % (self._x, self._y)
def __add__(self, other):
'''
addition
'''
return Vector(self._x + other._x, self._y + other._y)
def __sub__(self, other):
'''
subtraction
'''
return Vector(self._x - other._x, self._y - other._y)
# Call
v1 = Vector(8, 2)
v2 = Vector(2, 4)
# Calling the __sub__ function
print(v1-v2) # prints Vector(6, -2)
Works just like the add function, except you activate it by using the - sign.
mul
class Vector():
'''
Create a vector, input type = Vector(x, y)
'''
def __init__(self, *args)
if len(args) <= 1:
self._x, self._y = 0
else:
self._x, self._y = args
def __repr__(self):
'''
returns the information of Vector in form Vector(x, y)
'''
return 'Vector(%r, %r)' % (self._x, self._y)
def __add__(self, other):
'''
addition
'''
return Vector(self._x + other._x, self._y + other._y)
def __sub__(self, other):
'''
subtraction
'''
return Vector(self._x - other._x, self._y - other._y)
def __mul__(self, y):
'''
multiplication
'''
return Vector(self._x * y, self._y * y)
# Call
v1 = Vector(8, 2)
print(v1 * 3) # prints Vector(24, 6)
Again, works just like the add function, except you activate it by using the * sign.
bool
class Vector():
'''
Create a vector, input type = Vector(x, y)
'''
def __init__(self, *args)
if len(args) <= 1:
self._x, self._y = 0
else:
self._x, self._y = args
def __repr__(self):
'''
returns the information of Vector in form Vector(x, y)
'''
return 'Vector(%r, %r)' % (self._x, self._y)
def __add__(self, other):
'''
addition
'''
return Vector(self._x + other._x, self._y + other._y)
def __sub__(self, other):
'''
subtraction
'''
return Vector(self._x - other._x, self._y - other._y)
def __mul__(self, y):
'''
multiplication
'''
return Vector(self._x * y, self._y * y)
def __bool__(self):
'''
valid vector
'''
return bool(max(self._x, self._y))
# Call
v1 = Vector(8, 2)
v2 = Vector()
print(bool(v1)) # prints True bool(positive integer==true)
print(bool(v2)) # prints False bool(0)==False
Activation : bool(instance)