Skip to main content

29.Python Inner Class

PYTHON INNER CLASS:
    In python object oriented programming,a class can include variables and methods.In addition to these,a class can also include another another class in it.In short,A class within another class is known as nested class.Inner class is the class defined within/inside another class.A class can have more than one inner classes.
For example,Imagine a market that consists of different products and each product has its own data and methods.Here Market is the outer class and Product is the inner class.
It is not mandatory to create inner class.You can also create Product as a separate class. Remember,each class has an object,so in order to create an object for an inner class,there are two methods
1.creating a object within the __init__() method of the outer class.
self.variableName=self.innerclassName()
2.creating the object outside the class
obj=outerclassName.innerclassName()
Syntax
Outer class:
    methods/variables
    Inner class:
        methods/variables

Program
class Market:#Outer class
    def __init__(self,name,place):#variable initialization
        self.name=name
        self.place=place
        self.pobj=self.Product(1,5.5)#inner class object creation
    def display(self):#Outer class method
        print("Name:",self.name,"\nPlace:",self.place)
    class Product:#Inner class
        def __init__(self,pid,price):#variable initialization
            self.pid=pid
            self.price=price
        def display(self):#Inner class method
            print(f'Product id:{self.pid}\nPrice:{self.price}')
name=input("Enter the name of the market")#user input
place=input("Enter the name of place")#user input
marketobj=Market(name,place)#Outer class object creation
marketobj.display()#accessing outer class method
marketobj.pobj.display()#accessing inner class method
Output
Enter the name of the market:EFGH
Enter the name of place:India 
Name: EFGH 
Place: India 
Product id:1 
Price:5.5
Note that,both the inner and outer class has a display() method.These are different.
Program
class Market:#Outer class
    def __init__(self,name,place):#variable initialization
        self.name=name
        self.place=place
    def display(self):#Outer class method
        print("Name:",self.name,"\nPlace:",self.place)
    class Product:#Inner class
        def __init__(self,pid,price):
            self.pid=pid
            self.price=price
        def display(self):#Inner class method
            print(f'Product id:{self.pid}\nPrice:{self.price}')
name=input("Enter the name of the market:")#user input
place=input("Enter the name of place:")#user input
marketobj=Market(name,place)#Outer class object creation
marketobj.display()#accessing outer class method
pobj=Market.Product(1,5)#Inner class object creation
pobj.display()#accessing inner class method
Output
Enter the name of the market:EFGH
Enter the name of place:India 
Name: EFGH 
Place: India 
Product id:1 
Price:5.5
Need to learn more about python programming language,Check out the video below!!

Happy Learning!!

Comments

Popular posts from this blog

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Bug Bounty Course

Thanks for your interest in our course Limited Offer This is a paid course, Rs 10000 ,Rs 1000  for Indian people, 75$ 15$ for international people , 50$ 10$ for Srilankan people Instructions to get course: Indian people can get the course by paying using this UPI id  tamilhacks@ybl  send 1000 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)    The above mentioned method is most preferable. if you don't have a upi id app, install phonepe or google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100Rs for tax and other charges here ) After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that. As for international peopl