Python style question: support class as inner class?

I like to use the structures built into the language to organize my code. But there is one situation where I cannot remain consistent, simply because I do not see the ultimate best way to do this. This applies to support classes, that is, classes that are used exclusively within another class: I make them inner classes or separate classes.

Inner classes:

class Complicated:
    class Utility1:
        pass
    class Utility2:
        pass
    pass

Separate classes:

class Complicated:
    pass

class Utility1:
    pass

class Utility2:
    pass

Inner classes have the advantage of being limited within a single class that uses them. But the problem is that I get less space for writing code due to indentation.

, . , , .

, - python , ? , " ", , - , .

+3
4

class Complicated:
    pass

class _Utility1:
    pass

class _Utility2:
    pass

. _ , ( from module import *, ).

: PEP 8:

.

+6

, python. , python:

class Complicated:
    pass

class _Utility1:
    pass

class _Utility2:
    pass
+1

, , , :

class Foo(object):
    normalize = str

    def process(self, input):
        # Do some stuff in here...
        return self.normalize(output)

class UniFoo(Foo):
    normalize = unicode

class TotallyMungedFoo(Foo):
    class normalize(object): ...

( ) , , "" __all__.

0

Python

Python . , Java, Python .

Work on this issue: Define all inner classes with a parent parameter. This parameter can be used to access attributes and methods of the outer class.

Python example class with inner class

class Outer:
    def __init__(self):
        self.myVar = 100
        self.inner = self.Inner(self)

    def getDoubleVar(self):
        return(self.myVar * 2)

    #Define inner class
    class Inner:
        def __init__(self, parent):
            self.parent = parent

        #Use the parent variable to access parent class attributes/methods
        def printOuterVar(self):
            print(self.parent.myVar)

        def callOuterMethod(self):
            return(self.parent.getDoubleVar())




#Create Instance of Outer Class
outer = Outer()

#Display myVar
print("Display myVar")
print(outer.myVar)


#Execute Outer Method
print("\nExecute Outer Method")
print("val = outer.getDoubleVar()")
val = outer.getDoubleVar()
print("val: {0}".format(val))

#Execute Inner Method
print("\nExecute Inner Method")
print("outer.inner.printOuterVar()")
outer.inner.printOuterVar()

#Execute Inner Method That Calls Outer Method
print("\nExecute Inner Method That Calls Outer Method")
val = outer.inner.callOuterMethod()
print("val = outer.inner.callOuterMethod()")
print("val: {0}".format(val))

#Create Instance of Inner Class Separately
print("\nInstantiate Inner Class Separately")
#Note that you provide the current outer instance as the parent parameter
print("Note that you provide the current outer instance as the parent parameter")
print("myInner = outer.Inner(outer)")
myInner = outer.Inner(outer)

#Call Inner Method
print("\nCall Inner Method")
print("myInner.printOuterVar()")
myInner.printOuterVar()
print("finished")
0
source

Source: https://habr.com/ru/post/1786385/


All Articles