Python,在缩进级别和PEP-8宽度限制之间挤压

时间:2015-12-16 15:38:30

标签: python class nested indentation pep

我写了很长很整洁的Python类:

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)

现在,事实证明我需要动态创建这个大类,以便它的一些静态成员与它的一个实例不同。 简而言之,我需要把它变成像:

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)

因此它将打破PEP-8的安排。我仍然可以通过审查中的所有内容,并将线条分解为更多的部分,以便它仍然适合。但这很乏味,我觉得这是我不应该担心的事情,因为它对我的实际编码活动没什么用。

我该怎么办?有没有办法实现这一点,而无需添加另一级别的缩进?喜欢使用装饰器?或者是否有神奇的替代方法来划分python块?特殊字符可能吗?

我愿意编写符合标准的易于编辑的*.py文件,但有时这是一个令人费解的目标:\

2 个答案:

答案 0 :(得分:3)

您可以使用继承:

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    // Make sure the session on this thread matches.
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionAllowBluetooth error:nil];

    // Here is the main connundrum: I want to set AVAudioSessionModeDefault so that the MPVolumeView will work,
    // but when I do that the mic picks up the audio from the speaker and plays it back creating an unwanted feeback loop.
    // If I set the mode to AVAudioSessionModeVoiceChat the voice processing (primarily the AEC) fixes the feeback loop,
    // but then the MPVolumeView no longers works.  (It no longer adjusts the system volume.
    //Question: How can I have BOTH the voice processing AND the MPVolumeView adjust the system volume?
    if(!appDelegate.viewController.voiceChatMode)
        [audioSession setMode:AVAudioSessionModeDefault error:nil];
    else
        [audioSession setMode:AVAudioSessionModeVoiceChat error:nil];

    [audioSession setPreferredIOBufferDuration:0.005 error:nil];
    [audioSession setActive:YES error:nil];

子类不应该有缩进问题,因为它非常小。

答案 1 :(得分:2)

您也可以通过直接指定__doc__来指定类的文档字符串。

doc_of_foo = '''
Very long text, very long text, very long text
'''

def make_foo():
    def one_more_level_for_good_measure():
        class Foo(object):
            __doc__ = doc_of_foo

        return Foo
    return one_more_level_for_good_measure()

Foo = make_foo()

# help(Foo) → Very long text ...