Ano*_*ous 44 python refactoring pylint
我想将一个大的Python函数重构为较小的函数.例如,请考虑以下代码段:
x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
Run Code Online (Sandbox Code Playgroud)
当然,这是一个微不足道的例子.在实践中,代码更复杂.我的观点是它包含许多必须传递给提取函数的局部范围变量,它们可能如下所示:
def mysum(x1, x2, x3, x4, x5, x6, x7, x8, x9):
x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
return x
Run Code Online (Sandbox Code Playgroud)
问题是pylint会触发有关太多参数的警告.做以下事情可以避免警告:
def mysum(d):
x1 = d['x1']
x2 = d['x2']
...
x9 = d['x9']
x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
return x
def mybigfunction():
...
d = {}
d['x1'] = x1
...
d['x9'] = x9
x = mysum(d)
Run Code Online (Sandbox Code Playgroud)
但是这种方法对我来说太丑了,它需要编写很多甚至是多余的代码.
有没有更好的方法呢?
小智 92
"如果你有一个包含10个参数的程序,你可能会错过一些."
10个论点中的一些可能是相关的.将它们分组为一个对象,然后传递它.
举个例子,因为问题中没有足够的信息可以直接回答:
class PersonInfo(object):
def __init__(self, name, age, iq):
self.name = name
self.age = age
self.iq = iq
Run Code Online (Sandbox Code Playgroud)
然后你的10个参数函数:
def f(x1, x2, name, x3, iq, x4, age, x5, x6, x7):
...
Run Code Online (Sandbox Code Playgroud)
变为:
def f(personinfo, x1, x2, x3, x4, x5, x6, x7):
...
Run Code Online (Sandbox Code Playgroud)
并且来电者更改为:
personinfo = PersonInfo(name, age, iq)
result = f(personinfo, x1, x2, x3, x4, x5, x6, x7)
Run Code Online (Sandbox Code Playgroud)
pax*_*blo 41
你想要一个更好的方法来传递参数,或者只是一种方法来阻止pylint
你给你带来困难吗?如果是后者,我似乎记得你可以通过pylint
在代码中按照以下方式放置控制注释来阻止唠叨:
#pylint: disable-msg=R0913
Run Code Online (Sandbox Code Playgroud)
要么:
#pylint: disable-msg=too-many-arguments
Run Code Online (Sandbox Code Playgroud)
记得在切实可行的情况下尽快将它们重新打开.
在我看来,传递大量论据并没有什么本质上的错误,并且主张在一些容器论证中包装它们的解决方案并没有真正解决任何问题,除了pylint
不要唠叨你:-).
如果你需要传递20个参数,那么传递它们.可能这是必需的,因为你的功能太多而且重新分解可以帮助那些,这是你应该看的东西.但它不是一个决定,我们真的可以,除非我们看到了"真实"的代码是什么.
Nad*_*mli 24
您可以轻松更改pylint中允许的最大参数数量.只需打开你的pylintrc文件(如果你还没有它就生成它)并更改:
MAX-ARGS = 5
至:
max-args = 6#或任何适合您的值
从pylint的手册
指定适合您的设置和编码标准的所有选项可能很繁琐,因此可以使用rc文件指定默认值.Pylint查找/ etc/pylintrc和〜/ .pylintrc.--generate-rcfile选项将根据标准输出和退出的当前配置生成注释配置文件.您可以在此配置之前添加其他选项以在配置中使用它们,或者从默认值开始并手动调整配置.
hbw*_*hbw 12
您可以尝试使用Python的变量参数功能:
def myfunction(*args):
for x in args:
# Do stuff with specific argument here
Run Code Online (Sandbox Code Playgroud)
对于 Python 3,您应该只使用仅关键字参数:
"""Example of a function causing pylint too-many-arguments"""
def get_car(
manufacturer, model, year=None, registration_number=None, vin=None, color=None
):
"""Returns dict with all required car attributes"""
return {
"manufacturer": manufacturer,
"model": model,
"year": year,
"registration_number": registration_number,
"vin": vin,
"color": color,
}
print(repr(get_car(manufacturer="ACME", model="Rocket")))
Run Code Online (Sandbox Code Playgroud)
"""Example of a function causing pylint too-many-arguments"""
def get_car(
manufacturer, model, year=None, registration_number=None, vin=None, color=None
):
"""Returns dict with all required car attributes"""
return {
"manufacturer": manufacturer,
"model": model,
"year": year,
"registration_number": registration_number,
"vin": vin,
"color": color,
}
print(repr(get_car(manufacturer="ACME", model="Rocket")))
Run Code Online (Sandbox Code Playgroud)
"""Show how to avoid too-many-arguments"""
def get_car(
*, manufacturer, model, year=None, registration_number=None, vin=None, color=None
):
"""Returns dict with all required car attributes"""
return {
"manufacturer": manufacturer,
"model": model,
"year": year,
"registration_number": registration_number,
"vin": vin,
"color": color,
}
print(repr(get_car(manufacturer="ACME", model="Rocket")))
Run Code Online (Sandbox Code Playgroud)
pylint pylint_args_too_many.py
************* Module pylint_args_too_many
pylint_args_too_many.py:4:0: R0913: Too many arguments (6/5) (too-many-arguments)
------------------------------------------------------------------
Your code has been rated at 6.67/10 (previous run: 6.67/10, +0.00)
Run Code Online (Sandbox Code Playgroud)
简化或分解函数,使其不需要九个参数(或忽略pylint,但像你提议的那样闪避一个lint工具的目的).
编辑:如果这是一个临时措施,请使用此处所述的评论禁用相关特定功能的警告:http://lists.logilab.org/pipermail/python-projects/2006-April/000664.html
稍后,您可以查看所有已禁用的警告.
我不喜欢引用数字,符号名称更具表现力,并且避免添加可能会随着时间的流逝而过时的注释。
所以我宁愿这样做:
#pylint: disable-msg=too-many-arguments
Run Code Online (Sandbox Code Playgroud)
而且我还建议不要让它悬在那里:它会一直保持活动状态,直到文件结束或被禁用,以先到者为准。
所以更好地做:
#pylint: disable-msg=too-many-arguments
code_which_would_trigger_the_msg
#pylint: enable-msg=too-many-arguments
Run Code Online (Sandbox Code Playgroud)
我还建议每行启用/禁用一个警告/错误。
归档时间: |
|
查看次数: |
74746 次 |
最近记录: |