Python,压缩在缩进级别和 PEP-8 宽度限制之间
Python, squeezed between indentation levels and PEP-8 width limit
我写的很长很工整Pythonclass:
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)
现在,我发现我需要动态创建这么大的 class,这样它的一些静态成员就会因它的一个实例而异。
简而言之,我需要把它变成这样的东西:
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 协议。我仍然可以通过审查中的所有内容并将行分解成更多部分,以便它仍然适合。但这很乏味,我觉得这是我不应该担心的事情,因为它与我的实际 coding activity.[=14 没什么可看的=]
我该怎么办?有没有办法在不需要添加另一层缩进的情况下实现这一目标?喜欢使用装饰器?或者是否有神奇的替代方法来分隔 python 块?可能是特殊字符?
我愿意编写符合标准的 和 易于编辑的 *.py
文件,但有时这是一个令人费解的目标 :\
您也可以通过直接分配 __doc__
.
来指定 class 的文档字符串
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 ...
您可以使用继承:
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
childclass应该不会有缩进问题,因为它很小。
我写的很长很工整Pythonclass:
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)
现在,我发现我需要动态创建这么大的 class,这样它的一些静态成员就会因它的一个实例而异。 简而言之,我需要把它变成这样的东西:
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 协议。我仍然可以通过审查中的所有内容并将行分解成更多部分,以便它仍然适合。但这很乏味,我觉得这是我不应该担心的事情,因为它与我的实际 coding activity.[=14 没什么可看的=]
我该怎么办?有没有办法在不需要添加另一层缩进的情况下实现这一目标?喜欢使用装饰器?或者是否有神奇的替代方法来分隔 python 块?可能是特殊字符?
我愿意编写符合标准的 和 易于编辑的 *.py
文件,但有时这是一个令人费解的目标 :\
您也可以通过直接分配 __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 ...
您可以使用继承:
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
childclass应该不会有缩进问题,因为它很小。