Learn to Code via Tutorials on Repl.it

← Back to all posts
Small Guide for Object Orientated Programming
h
ubuntulove2004 (1)

Welcome to The Tiny OOP Python Guide!

Hello!
I put this small guide together to help people learn the basics of OOP. All of the guide is functioning code.

Chapter 1

Learning how classes function and how to assign them

class Potato:

    #Here p is a class attribute

    p = 1

o = Potato()

print(o.p)

Learning the init function:

Note: The init() function is called automatically every time the
class is being used to create a new object.

class OrangeJuice:

    def __init__(self,colour,taste):

	    self.colour = colour

	    self.taste = taste

drink = OrangeJuice("Orange","sour")

print(drink.colour,"juice is",drink.taste)

Learning Object methods

Note: The self parameter is a reference to the current instance of the
class, and is used to access variables that belong to the class.

Note: The self parameter is a reference to the current instance of the
class, and is used to access variables that belongs to the class. It
does not have to be named self , you can call it whatever you like,
but it has to be the first parameter of any function in the class.

class OrangeJuice:

    def __init__(self,colour,taste):

	    self.colour = colour

	    self.taste = taste

    def gulp(self):

	    print("Gulp!")


drink = OrangeJuice("Orange","sour")

drink.gulp()

Modify object properties

class OrangeJuice:

    def __init__(self,colour,taste):

	    self.colour = colour

	    self.taste = taste

    def gulp(self):

	    print("Gulp!")

  
drink = OrangeJuice("Orange","sour")


drink.taste = "sweet"


print(drink.colour,"juice is",drink.taste)

Deleting object properties

Note: to remove an object property use 'del'

class OrangeJuice:

    def __init__(self,colour,taste):

	    self.colour = colour

	    self.taste = taste

    def gulp(self):

	    print("Gulp!")

  
drink = OrangeJuice("Orange","sour")

del drink.colour

Chapter 2

Creating a Parent Class

Note: Create a class named OrangeJuice, with colour and taste
properties, and a printname method:

class OrangeJuice:

    def __init__(self,co,ta):

	    self.colour = co

	    self.taste = ta

    def gulp(self):

	    print(self.colour,self.taste)
  
drink = OrangeJuice("Orange","sour")

drink.gulp()

Creating a Child Class

class OrangeJuice:

    def __init__(self,co,ta):

	    self.colour = co

	    self.taste = ta

    def gulp(self):

	    print(self.colour,self.taste)

class MyDrink(OrangeJuice):

	pass


drink2 = MyDrink("Orange","sour")

drink2.gulp()

Note: Use the pass keyword when you do not want to add any other
properties or methods to the class.

Adding the init function to the Child class

class OrangeJuice:

    def __init__(self,co,ta):

	    self.colour = co

	    self.taste = ta

    def gulp(self):

	    print(self.colour,self.taste,self.flavour,self.temperature)

 
class MyDrink(OrangeJuice):

    def __init__(self,co,ta,flav,temp):

	    self.temperature = temp

	    self.flavour = flav

Note: The child's init() function overrides the inheritance of the
parent's init() function. To keep the inheritance of the parent's
init() function, add a call to the parent's init() function

Note: Python also has a function to inherit all of the inheritance from the parent class. To do that use super().init(arguments)

OrangeJuice.__init__(self,co,ta)

super().__init__(co,ta)

drink2 = MyDrink("Orange","sour","Orange","Orangy")

drink2.gulp()

class OrangeJuice:

    def __init__(self,co,ta):

	    self.colour = co

	    self.taste = ta

    def gulp(self):

	    print(self.colour,self.taste,self.flavour,self.temperature)

  

class MyDrink(OrangeJuice):

    def __init__(self,co,ta,flav,temp):

	    self.temperature = temp

	    self.flavour = flav

		super().__init__(co,ta)

    def say(self,flavour,taste):

		 return  "{} is {} ".format(self.flavour,  self.taste)


drink2 = MyDrink("Orange","sour","Orange","Orangy")

drink2.gulp()

print(drink2.say("Orange","sour"))

Thats all folks!

Thank you for reading my guide! For any improvements please comment down below!