首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python,压缩在缩进级别和PEP-8宽度限制之间。

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

Stack Overflow用户
提问于 2015-12-16 15:38:29
回答 2查看 291关注 0票数 1

我编写了一个相当长的、整洁的Python类:

代码语言:javascript
复制
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)

现在,我需要动态地创建这个大类,这样它的一些静态成员就会在不同的实例中有所不同。简单来说,我需要把它变成这样的东西:

代码语言:javascript
复制
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文件,但有时这是一个令人费解的目标:\

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-17 23:29:18

您可以使用继承:

代码语言:javascript
复制
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

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

票数 3
EN

Stack Overflow用户

发布于 2015-12-16 15:52:54

您也可以通过直接赋值__doc__来指定类的docstring。

代码语言:javascript
复制
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 ...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34316107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档