Python Class and Objects Example



Python Class and Objects example:-


1) Write code sample.py:-

# Sample class with init method

class Person:



    # init method or constructor

    def __init__(self, name):

        self.name = name



    # Sample Method

    def say_hi(self):

        print('Hello, my name is', self.name)



p = Person('Om')

p.say_hi()


Output:-

Hello, my name is Om




2) Class and Instance Variables


# Python3 program to show that the variables with a value

# assigned in the class declaration, are class variables and

# variables inside methods and constructors are instance

# variables.



# Class for Dog



class Dog:



    # Class Variable

    animal = 'dog'



    # The init method or constructor

    def __init__(self, breed, color):



        # Instance Variable

        self.breed = breed

        self.color = color



# Objects of Dog class

Rodger = Dog("Pug", "brown")

Buzo = Dog("Bulldog", "black")



print('Rodger details:')

print('Rodger is a', Rodger.animal)

print('Breed: ', Rodger.breed)

print('Color: ', Rodger.color)



print('\nBuzo details:')

print('Buzo is a', Buzo.animal)

print('Breed: ', Buzo.breed)

print('Color: ', Buzo.color)



# Class variables can be accessed using class

# name also

print("\nAccessing class variable using class name")

print(Dog.animal)






Output:-


Rodger details:

Rodger is a dog

Breed:  Pug

Color:  brown


Buzo details:

Buzo is a dog

Breed:  Bulldog

Color:  black


Accessing class variable using class name

dog



3)Defining instance variables using the normal method.


# Python3 program to show that we can create

# instance variables inside methods



# Class for Dog



class Dog:



    # Class Variable

    animal = 'dog'



    # The init method or constructor

    def __init__(self, breed):



        # Instance Variable

        self.breed = breed



    # Adds an instance variable

    def setColor(self, color):

        self.color = color



    # Retrieves instance variable

    def getColor(self):

        return self.color



# Driver Code

Rodger = Dog("pug")

Rodger.setColor("brown")

print(Rodger.getColor())






Output:-

brown



4)Inheritance  example :-

# A Python program to demonstrate inheritance



# Base or Super class. Note object in bracket.

# (Generally, object is made ancestor of all classes)

# In Python 3.x "class Person" is

# equivalent to "class Person(object)"



class Person(object):



    # Constructor

    def __init__(self, name):

        self.name = name



    # To get name

    def getName(self):

        return self.name



   



# Inherited or Subclass (Note Person in bracket)

class Employee(Person):



    # Here we return true

    def isEmployee(self):

        return True



# Driver code

emp = Person("om1") # An Object of Person

print(emp.getName())



emp = Employee("om2") # An Object of Employee

print(emp.getName(), emp.isEmployee())






Output:-

om1

om2 True


5)Subclassing (Calling constructor of parent class)

# Python code to demonstrate how parent constructors

# are called.



# parent class

class Person(object):



    # __init__ is known as the constructor

    def __init__(self, name, idnumber):

        self.name = name

        self.idnumber = idnumber



    def display(self):

        print(self.name)

        print(self.idnumber)



# child class



class Employee(Person):

    def __init__(self, name, idnumber, salary, post):

        self.salary = salary

        self.post = post



        # invoking the __init__ of the parent class

        Person.__init__(self, name, idnumber)



# creation of an object variable or an instance

a = Employee('Rahul', 886012, 200000, "Intern")



# calling a function of the class Person using its instance

a.display()






Output:-

Rahul

886012





Previous
Next Post »