Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python - Inner Classes



Inner Class in Python

A class defined inside another class is known as aninner class in Python. Sometimes inner class is also called nested class. If the inner class is instantiated, the object of inner class can also be used by the parent class. Object of inner class becomes one of the attributes of the outer class. Inner class automatically inherits the attributes of the outer class without formally establishing inheritance.

Syntax

class outer:   def __init__(self):      pass   class inner:      def __init__(self):         pass

An inner class lets you group classes. One of the advantages of nesting classes is that it becomes easy to understand which classes are related. The inner class has a local scope. It acts as one of the attributes of the outer class.

Example

In the following code, we have student as the outer class and subjects as the inner class. The __init__() constructor of student initializes name attribute and an instance of subjects class. On the other hand, the constructor of inner subjects class initializes two instance variables sub1, sub2.

A show() method of outer class calls the method of inner class with the object that has been instantiated.

class student:   def __init__(self):      self.name = "Ashish"      self.subs = self.subjects()      return   def show(self):      print ("Name:", self.name)      self.subs.display()   class subjects:      def __init__(self):         self.sub1 = "Phy"         self.sub2 = "Che"         return      def display(self):         print ("Subjects:",self.sub1, self.sub2)         s1 = student()s1.show()

When you execute this code, it will produce the followingoutput

Name: AshishSubjects: Phy Che

It is quite possible to declare an object of outer class independently, and make it call its own display() method.

sub = student().subjects().display()

It will list out the subjects.

Types of Inner Class

In Python, inner classes are of two types −

  • Multiple Inner Class
  • Multilevel Inner Class

Multiple Inner Class

Inmultiple inner class, a single outer class contains more than one inner class. Each inner class works independently but it can interact with the members of outer class.

Example

In the below example, we have created an outer class namedOrganization and two inner classes.

class Organization:   def __init__(self):      self.inner1 = self.Department1()      self.inner2 = self.Department2()           def showName(self):      print("Organization Name: Tutorials Point")   class Department1:      def displayDepartment1(self):         print("In Department 1")               class Department2:      def displayDepartment2(self):         print("In Department 2")# instance of OuterClassouter = Organization() # Calling show methodouter.showName()  # InnerClass instance 1inner1 = outer.inner1 # Calling display methodinner1.displayDepartment1() # InnerClass instance 2inner2 = outer.inner2 # Calling display methodinner2.displayDepartment2()

On executing, this code will produce the followingoutput

Organization Name: Tutorials PointIn Department 1In Department 2

Multilevel Inner Class

It refers to an inner class that itself contains another inner class. It creates multiple levels of nested classes.

Example

The following code explains the working of Multilevel Inner Class in Python −

class Organization:   def __init__(self):      self.inner = self.Department()   def showName(self):      print("Organization Name: Tutorials Point")   class Department:      def __init__(self):         self.innerTeam = self.Team1()      def displayDep(self):         print("In Department")      class Team1:         def displayTeam(self):            print("Team 1 of the department")# instance of outer class                outer = Organization()  # call the method of outer classouter.showName()  # Inner Class instanceinner = outer.inner  inner.displayDep()  # Access Team1 instanceinnerTeam = inner.innerTeam  # Calling display methodinnerTeam.displayTeam()

When you run the above code, it will produce the belowoutput

Organization Name: Tutorials PointIn DepartmentTeam 1 of the department
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp