Object-oriented Python

in #utopian-io8 years ago (edited)

What Will I Learn?

  • Object-oriented concept
  • Classes and objects
  • Inheritance

Requirements

  • A PC/laptop with any Operating system such as Linux, Mac OSX, Windows OS
  • Preinstalled Python
  • Preinstalled Code Editors such as Atom, Sublime text or Pycharm IDE

Note: This tutorial is performed in Pycharm IDE in laptop with Ubuntu 17.1, 64 bit OS

Difficulty

Intermediate, anyone with basic programming knowledge of python or any other programming language can catch up this tutorial. I recommend learning basics of python programming before starting this tutorial. The links for basic tutorials are at the bottom of this tutorial.

Tutorial Contents

Object-oriented programming:
Object-oriented programming deals with object and classes rather than procedure or logic in programming. While writing large programs we use object-oriented programming rather the procedure-oriented programming. Before in basic pythons tutorials, we programmed in a procedure-oriented way. Some of the most used terms in object-oriented programming are discussed below:

Class: Classes defines class variables, instance variables, and methods which determines the characteristic of the object of the class. Class variables, instance variables, and methods combinely known as attributes of the class.
A class is created by keyword class followed by name of the class which is also followed by a colon(:).

class ClassName:
    statement 1
    statement 2

Object: Object is defined as an instance of the class.

Instance: For a defined class, the instance is the individual object of that particular class.
Instance of a class is created by calling the class with it's name and passing
the argument in it.

instance = classname(argument)

Instance variable It is defined inside a function within a class and is used only by its current instance.

Class variables: Variables which are defined within a class and are accessed by any of the class instances is known as class variables.

Methods: Methods are functions defined under a class.

# creating class
class Student:
    # defining methods
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
    def displayStudent(self):
        print "Name : ", self.name, ", Roll: ", self.roll
# creating first object of Student class
st1 = Student("Programming", 1)
# creating second object of Student class
st2 = Student("Hub", 2)
# calling method using object of the class
st1.displayStudent()
st2.displayStudent()

In above code, class Student is created and two methods _ init _ and displayStudent is defined.
_ init _ is a special type of method in python classes. Whenever a new object is created python calls this method. This method is used to initialize the object. We don't have to explicitly call _ init _ method while creating new objects. displayStudent method is defined to print name and roll number of student. Both of the methods are passed with self parameter, which is provided by default by python. Methods that are defined within a class must contain the self as first parameter, this differentiates other methods with class methods.st1 and st2 are two objects of the class Student. Class method displayStudent is called at last using objects two display the name and roll number of student.

Output:

Name :  Programming , Roll:  1
Name :  Hub , Roll:  2

Inheritance:
Inheritance allows us to reuse the code. We can inherit the feature of one class to another.

class A:  # defining class A
.
.
.
class B(A): # defining class B which is  subclass of A
.
.
.
class C(A, B): # defining class C which is  subclass of A and B
.
.
.

Class A is the base class or parent class. Class B is the child or subclass of A which means some of the characteristic of A is inherited in B. Class C is the subclass of A and B.

# defining parent class College
class College:
    def __init__(self, name, mob_num):
        self.name = name
        self.mob_num = mob_num
    def display(self):
        '''Tell my details.'''
        "Name : ", self.name, ", Mobile number: ", self.mob_num
# defining class Teacher which is child class of College
class Teacher(College):
    def __init__(self, name, mob_name, salary):
        College.__init__(self, name, mob_name)
        self.salary = salary
    def display(self):
        College.display(self)
        print "Name : ", self.name, ", Mobile number: ", self.mob_num, ", Salary:", self.salary
# defining class Student which is child class of College
class Student(College):
    def __init__(self, name, mob_num, grade):
        College.__init__(self, name, mob_num)
        self.grade = grade
  def display(self):
        College.display(self)
        print "Name : ", self.name, ", Mobile number: ", self.mob_num, ", Grade:", self.grade
t = Teacher('Python Sir', 1235634, 20000)
s = Student('James', 567765, 'A')
t.display()
s.display()

in above example, name and mobile number of student and teacher are extended from parent class College. By deriving this we don't have to write name and mobile number in each classes. Features are derived and added which wasn't in parent class. salary is added to Teacher class and and grade is added in Student class.

Output:

Name :  Python Sir , Mobile number:  1235634 , Salary: 20000
Name :  James , Mobile number:  567765 , Grade: A

For more details visit Python Docs.

Above codes is in my github. Click here to download.

Curriculum

Link to previous tutorials.

Python tutorials for beginners : Part - I

Python tutorials for beginners : Part - II

Python tutorials for beginners : Part - III



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hey @programminghub I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thank you for the contribution. It has been approved.

@creon thanks, this is the part of my tutorial series thus, I am posting this. I will cover more advance topic in future.

Coin Marketplace

STEEM 0.04
TRX 0.31
JST 0.074
BTC 63305.46
ETH 1662.87
USDT 1.00
SBD 0.41