Search code examples
pythonclassnestedindentationpep

Python, squeezed between indentation levels and PEP-8 width limit


I have written quite a long, neat Python class:

class Foo(object):                     |    |
      """It is long and it has to deal |    |
      with PEP8 code width requirements|    |
      which are 72 characters for      |    |
      comments and docstrings and 79   |    |
      for actual code.                 |    |
      """                              |    |
      class_parm = 12                  |    |
      pass # after several long, broken down|
           # into several pieces, but in the|
           # end *fit* lines of script, phew|
                                            ^
                                           79 characters (say)
                                       ^    
                                      72 characters (say)

Now, it turns out that I need this big class to be dynamically created, so that some of its static members will be different from one instance of it to another. It a nutshell, I need to turn it into something like:

def make_foo(parm):                   
    class Foo(object):                 |    |
          """It is long and it has to d|al  |    
          with PEP8 code width requirem|nts |    
          which are 72 characters for  |    |    
          comments and docstrings and 7|    |    
          for actual code.             |    |    
          """                          |    |    
          class_parm = parm            |    |    
          pass # after several long, broken |own
               # into several pieces, but in|the
               # end *fit* lines of script, |hew
    return Foo                              |
                                            ^
                                           79 characters (say)
                                       ^    
                                      72 characters (say)

And so it will break the PEP-8 arangement. I can still pass everything in review and break down the lines into more pieces so that it'll still fit. But it is tedious and I feel like this is something I shouldn't have worry about since it doesn't have much to see with my actual coding activity.

What should I do? Is there a way to achieve this without needing to add another level of indentation? Like using a decorator? Or are there magical alternative ways to delimit python blocks? Special characters maybe?

I am willing to write both standard-compliant and easy-to-edit *.py files, but it sometimes is quite a puzzling goal :\


Solution

  • You can use inheritance:

    class Foo(object): 
        """It is long and it has to deal
        with PEP8 code width requirements
        which are 72 characters for
        comments and docstrings and 79
        for actual code. 
        """                         
        class_parm = 12
        pass # after several long, broken down|
           # into several pieces, but in the|
           # end *fit* lines of script, phew|
    
    
    def make_foo(parm):                   
        class Foo2(Foo):
            """Foo without indentation problems."""
            class_parm = parm
        return Foo2
    

    The child class should not have indentation problems because its very small.