开发者

Why say x = x in Python?

In this file, in the function cross_from_below(x, threshold), there is a line that says threshold = threshold. What's the point of this line? Does t开发者_如何学运维his do something differently than if this command weren't there?


There is no point to that assignment. It's probably just left over and should be removed. The next function is nearly identical, and doesn't have it.


No, in the specific example you quote, that line is (harmless but) redundant -- indeed the very next function (cross threshold from above rather than from below) is almost identical (save of course for the direction of the comparisons), lacks that extra assignment, yet works just the same way.

The one and only case where something like threshold=threshold would make a difference is in a def statement, where it's the idiom to force early binding of a name (it makes use of the fact that default values are evaluated at def time, i.e., "early";-). But that's not what that code is doing -- just adding it for completeness.


It could mean something. For example, if threshold was a property of a class, then assigning it could call a setter method.

But in this case, I think it's a typo. I've looked at the changelog and there is no hint that this was intentional for any reason.


Or it could be one of those cases where it made sense at some point to write something like :

 self.threshold = threshold

because the function was a method of some class that needed to kept the threshold. (In which case, it would mean something else, wouldn't it ?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜