Definition
A class is the blueprint from which individual objects are created
Each object that is created from a class is called an instance of the class.
A method that returns a value from a class's attribute but does not change it is called an accessor method.
A method that changes the value of a data attribute is called a mutator method.
Syntax
class classname:
...
def __init__(self, parameters): #instantiation
...
def __str__(self): #string representation of an object
...
def methodName(self,parameters): #other methods
...
Example
class Fraction:
def __init__(self,num,den):
self.numerator = num
self.denominator = den
def __str__(self):
return str(self.numerator) + " / " + str(self.denominator)
def getValue(self):
return self.numerator / self.denominator
#demo
x = Fraction(1,2) # create an object from the Fraction class with numerator = 1 and denominator = 2
print(x) # output 1 / 2
print(x.getValue()) # use the getValue function to get the value of x in decimal form