What is the use of nested classes in python? for which I would use something like that?

Asked

Viewed 35 times

-1

I can’t find much python-like, I know you can do that and I know how to do it, but I don’t know why anyone would want to do that, since the inner class is unable to access the attributes and methods of the outer class, Which is something I would normally use an inheritance to sort out, but other than the gambling inheritance, does using that come in handy? I saw my teacher doing a course in Django that I am currently doing.

  • This question if well worked has the potential to become a very good question and generate good answers. Asking carefully about the usefulness of nested classes is undefined scope, perhaps associating what are the advantages of using nested classes to a specific Pattern design, for example Builder, along with an example of use.

1 answer

1

I don’t know exactly in Python, but this is a general computing problem.

Usually class nested to organize the code.

imagine you have a class GoogleAPIClient, and say the answers you want to store in an object Response

This object Response is not an object Response any but dedicated, and if you name this class with a common name, it may collide with another Response otherwise.

You can name this Sponse as GoogleAPIClientResponse, but what you do is you usually nest, like, inside your GoogleAPIClient you reference this only as Response.

Some languages still allow nested classes to be private, i.e., you cannot generate an instance of this Answer outside of GoogleAPIClient.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.