Constructors

class NumberComplex:
    class ComplexNumbers:
        def __init__(self,realnum = o,i = o):
            self.real = realnum
            self.imaginarynum = i
        def getData(self):
            print("{}+{1}j".format(self.realnumber,self.imaginarynum))
complex1 = NumberComplex(2,3)
complex1.getData()
complex2 = NumberComplex(5)
complex2.attribute = 10
print((complex2.realnumber, complex2.imaginarynumber, complex2.attribute))
complex1.attribute

More Class Examples

class Tilapia:
    def swim(self):
        print("Tilapia cannot swim")
    def fly(self):
        print("Tilapia cannot fly")
    def jump(self):
        print("Tilapia can't fly")
class Shark:
    def jump(self):
        print("Shark can't fly")
    def swim(self):
        print("Shark can swim")
def jumping_test(fish):
    fish.jump()
bonny = Tilapia()
biggy = Shark()
jumping_test(bonny)
jumping_test(biggy)

Inheritance of Parent Object Classes

class Dog:
    def _init_(self):
        print("Dog is available")
    def whoisThis(self):
        print("Dog")
    def walk(self):
        print("Walks gently")
class Spitz(dog):    #child class
    def __init__(self):
        super().__init__()
        print("Spitz is now available")
    def whoisThis(self):
        print("Pitbull")
    def wag(self):
        print("Strong")
pitbull = Pitbull()
pitbull.whoisThis()
pitbull.walk()
pitbull.wag()

Object Methods

def _init_(self,model,insurance): #line fixed
    self.model = model
    self.insurance = insurance
  def ignite(self,ignite):
    return"{} ignites {}".format(self.model,ignition)
  def stop(self):
    return "{} is now stopping".format(self.model)
subaru = Car("Subaru","Insured")
print(subaru.ignite("'Fast'"))
print(subaru.stop())

Object/Class Instantiation

class Car:
    def _init_(self, model, insurance):
        self.model = model
        self.insurance = insurance
subaru = Car("Subaru","Insured")
toyota = Car("Toyoto","Uninsured")
print("Subaru is a {}".format(subaru._class_.car))
print("Toyoto is a {}".format(toyoto._class_.car))
print("{} is {}".format(subaru.model,subaru.insurance))
print("{} is {}".format(toyota.model,toyoto.insurance))

Working With Matplot Library

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas #Import figure canvas
from matplotlib.figure import Figure #import figure artist
fig = Figure()
canvas = FigureCanvas(fig)
#create 10000 random numbers using numpy
import numpy as np
x = np.random.randn(10000)

ax = fig.add_subplot(111) #create an axes artist

ax.hist(x, 100) #generate a histogram of the 10000 numbers

#add a title to the figure and save it
ax.set_title('Normal distribution with $\mu=0, \sigma=1$')
fig.savefig('matplotlib_histogram.png')