What is the purpose of inner class in Python?

0 votes
The inner or nested classes in Python are quite confusing. Is there anything that cannot be accomplished without them?
Feb 7 in Python by ana1504.k
• 7,890 points
311 views

1 answer to this question.

0 votes

Advantages of inner class:

  • Logical grouping of classes: If a class is useful to only one other class then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
  • Increased encapsulation: Consider two top-level classes A and B where B needs access to members of A that would otherwise be declared private. By hiding class B within class A A's members can be declared private and B can access them. In addition B itself can be hidden from the outside world.
  • More readable, maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.
answered Feb 7 by SDeb
• 13,210 points

Related Questions In Python

0 votes
1 answer

What is the purpose of using lambda functions in Python?

The main purpose of anonymous functions come ...READ MORE

answered Jun 11 in Python by Nisa
• 1,090 points
53 views
0 votes
1 answer

What is the purpose of hash function in python?

The hash() method returns the hash value of an object if it ...READ MORE

answered Aug 2 in Python by Mohammad
• 3,170 points
53 views
0 votes
0 answers
0 votes
1 answer

What is the purpose of else part in python exception handling?

When an error occurs, or exception as ...READ MORE

answered Aug 22 in Python by Arvind
• 2,960 points
36 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 412 views
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,210 points
83 views
0 votes
1 answer

What is the purpose of Zope Interfaces?

You can actually test if your object ...READ MORE

answered Feb 28 in Python by SDeb
• 13,210 points
59 views