Class definition within a function or another class

1

I've seen in some scripts definitions of classes within other classes or functions like this:

class Grok(object):

    class Foo(object):
        ...

    ...

What is the reason for this practice? Is it just to not allow direct instantiation of the internally defined class, and is this really a good practice or should I avoid its use at any cost?

class Circle:
        class DrawingAPIOne:
                '''Implementation-specific abstraction'''
                def drawCircle(self, x, y, radius):
                        print("API 1 is drawing a circle at ({}, {}) with radius {}".format(x, y, radius))
        class DrawingAPITwo:
                '''Implementation-specific abstraction'''
                def drawCircle(self, x, y, radius):
                        print("API 2 is drawing a circle at ({}, {}) with radius {}".format(x, y, radius))

        def __init__(self, x, y, radius):
                '''Implementation-independent abstraction; Initialize the necessary attributes'''
                self._x = x
                self._y = y
                self._radius = radius
        def drawWithAPIOne(self):
                '''Implementation-specific abstraction'''
                objectOfAPIone = self.DrawingAPIOne()
                objectOfAPIone.drawCircle(self._x, self._y, self._radius)
        def drawWithAPITwo(self):
                '''Implementation-specific abstraction'''
                objectOfAPItwo = self.DrawingAPITwo()
                objectOfAPItwo.drawCircle(self._x, self._y, self._radius)
        def scale(self, percent):
                '''Implementation-independent abstraction'''
                self._radius *= percent

I saw this code on the site: link

    
asked by anonymous 09.07.2018 / 06:46

1 answer

2

In general, the less the scope of something, the less it pollutes the code, the more it gets encapsulated and protected from misuse and generates less external concerns. Everything that is public has to take more care, not only with it itself, but anything that creates needs to be done in a way that does not conflict with what exists. In general scope this is becoming very complicated.

But Python does not work with class scope in the way other languages do. From what I understand it has even conflicting PEP on the subject. Then the protection does not occur. What makes some sense, after all Python has always been a script language, more recently it has tried to stop being and hence the best defined and protected scope to be more important.

That's why most of the time you create a class within such a small scope in an unprincipled language like Python, you're probably following good practice the way it's worse, not knowing why, without having a real reason for it. use it, you only do it because you saw someone doing it. And guess what? Many codes that you see this way is because the person just "followed good practice" without knowing why, without identifying if the context required so. Usually not good to use.

And beware of abstract examples, they usually show a mechanism, not with them and do in production.

This is an example that you can solve otherwise, using lambda , for example. In fact in the written form it does not even need this abstraction.

    
09.07.2018 / 06:53