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, 2019 in Python by ana1504.k
• 7,890 points
372 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, 2019 by SDeb
• 13,230 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, 2019 in Python by Nisa
• 1,090 points
66 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, 2019 in Python by Mohammad
• 3,210 points
62 views
0 votes
0 answers

What is the purpose of fit method in sklearn module in python?

I am new to machine learning. I ...READ MORE

Jul 5, 2019 in Python by Waseem
• 4,520 points
102 views
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, 2019 in Python by Arvind
• 2,980 points
47 views
0 votes
1 answer
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 548 views
0 votes
0 answers
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,230 points
102 views
0 votes
1 answer

What is the purpose of Zope Interfaces?

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

answered Feb 28, 2019 in Python by SDeb
• 13,230 points
68 views