当您想要的名称已被占用时,如何命名变量
对于这样的函数:
def example(things):
x1 = 1
x2 = 1
for thing in things:
(x2, y2) = some_function(thing)
x1 += x2
y1 += y2
return (x1, y1)
出现的问题是 x1
和 x2
代表相同的东西,y1
和 也代表同样的东西y2。假设您对
x1
和 y1
一无所知,那么对于如何命名 x2
和 y2
是否有任何经验法则在这种情况下?
在更简单的情况下(一个变量),我只需执行 x += some_function(in)
,但 Python 不会让我执行 (x, y) += recursive(in)
,所以我必须有这些。我怀疑这个名称并不是特别重要,但使用错误的变量名称会让我感到困扰。
在今天的例子中,变量是 ways
和 calls
,我只是在每个变量的前面附加了 r
:
def make_change(amount, coins):
# -- stuff -- #
if len(coins) > 1:
(rways, rcalls) = make_change(amount, coins[1:])
ways += rways
calls += rcalls
return (ways, calls)
With a function like this:
def example(things):
x1 = 1
x2 = 1
for thing in things:
(x2, y2) = some_function(thing)
x1 += x2
y1 += y2
return (x1, y1)
The problem that comes up is that x1
and x2
represent the same thing, and so do y1
and y2
. Assuming you know nothing about x1
and y1
, is there any rule of thumb for what to name x2
and y2
in this case?
In a simpler case (one variable), I would just do x += some_function(in)
, but Python won't let me do (x, y) += recursive(in)
, so I have to have these. I suspect the name isn't particularly important, but it bothers me using bad variable names.
In today's case, the variables were ways
and calls
and I just appended r
on the front of each:
def make_change(amount, coins):
# -- stuff -- #
if len(coins) > 1:
(rways, rcalls) = make_change(amount, coins[1:])
ways += rways
calls += rcalls
return (ways, calls)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您有两个具有相似内容的变量时,给它们命名以表达相似之处和不同之处。我不知道你在谈论“方式”和“调用”是什么,但看起来你正在做的是你有一个总交易金额,什么,交易金额?在这种情况下,我会将它们称为“ways_total”和“ways_tx”或类似的名称。
我绝对鼓励您不要做的就是在末尾添加“1”和“2”,或者故意拼错一个。
就在前几天,我正在查看一个计算运费的程序,我发现了三个名为“freight”、“freightcost”和“freightt”(最后的“t”加倍)的变量。这让我不知道这三者之间有什么区别。我必须深入研究该程序才能弄清楚。
我见过很多程序通过调用它们“freight1”和“freight2”来“解决”同样的问题。
如果您需要两个变量,它们之间必须存在一些差异,并且当您编写代码时,您必须知道差异是什么。给读者一个线索。
When you have two variables that have similar content, give them names that express what is similar and also what is different. I have no idea what you're talking about with "ways" and "calls", but it appears that what you're doing is you have a total and a, what, transaction amount? In that case, I'd call them "ways_total" and "ways_tx" or something like that.
What I would definitely encourage you to NOT do is just tack a "1" and a "2" on the end, or deliberately misspell one.
Just the other day I was looking at a program that calculates freight costs, and I found three variables named "freight", "freightcost", and "freightt" (the final "t" doubled). This gave me no clue what the difference between the three was. I had to dig through the program to figure it out.
I've seen plenty of programs that "solve" the same problem by calling them freight1 and freight2.
If you need two variables, there must be SOME difference between them, and when you're writing the code, you must know what that difference is. Give the reader a clue.
在这种特殊情况下,我会将您的
ways
和calls
命名为total_ways
和total_calls
并使用本地的有 'r' 只是没有 'r'。我认为这样的命名对于其他阅读此代码的人来说更具描述性。In this particular case I'd name your
ways
andcalls
astotal_ways
andtotal_calls
and would have the local ones with 'r' just without 'r'. I think such naming would be a bit more descriptive for someone else reading this code.您可以创建一个自定义结果对象,其中包含您想要跟踪的信息并定义一个 __add__() 方法,以便可以添加它们。
这看起来需要做很多工作,但它确实更清楚地表达了意图。
You could create a custom result object that contains the information you want to keep track of and defines an
__add__()
method so that they can be added.This seems like rather a lot of work to go to, but it does express the intent more clearly.