这个疯狂的循环和 if 序列是怎么回事?有改进这段代码的想法吗?
这段代码目前正在运行,但看起来很糟糕 - 并且可能在性能方面可以大大提高。
有什么建议吗?
def OnClick():
global Selection, touch, set_elsb, set_vreg, set_els, BAR_Items
A = viz.pick(0, viz.WORLD, all=False)
if touch != A: return
for i in BAR_Items:
if not set_els: break
elif BAR_Items[i] == A or SHAPES[i+"_SHP"] == A:
if i in Selection:
Selection.remove(i)
BAR_Items[i].clearActions()
VFrame.SetStatusText(frame, i + " has been deselected")
viz.director( do_chart )
else:
Selection.append(i)
非常感谢!
This code is currently working, but it looks terrible - and probably can be much improved in terms of performance.
Any Suggestions?
def OnClick():
global Selection, touch, set_elsb, set_vreg, set_els, BAR_Items
A = viz.pick(0, viz.WORLD, all=False)
if touch != A: return
for i in BAR_Items:
if not set_els: break
elif BAR_Items[i] == A or SHAPES[i+"_SHP"] == A:
if i in Selection:
Selection.remove(i)
BAR_Items[i].clearActions()
VFrame.SetStatusText(frame, i + " has been deselected")
viz.director( do_chart )
else:
Selection.append(i)
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然多了几行代码,但我认为它更清晰了。
整个
global
语句没有任何作用,因为您没有分配给它们中的任何一个。调用属性和设置键不需要global
关键字。It's a few more lines of code but I think it's clearer.
That entire
global
statement served no purpose as you weren't assigning to any of them. Calling attributes and setting keys don't require theglobal
keyword.我对此的正常方法是将其中一些重构为小方法。这通常使其更易于测试且更易于阅读。
My normal approach to this would be to re-factor some of it out into small methods. This generally makes it more testable and easier to read.
如果在此代码执行期间
set_els
未在外部更改,则:无论如何,当我的坏代码检测器看到这样的代码时,它会闪烁红灯,尤其是对于如此大量的全局变量。
If
set_els
is not changed outside during this code execution then:Anyway, my bad code detector flashes with red light when it sees such a code, especially with such amount of globals.
人们普遍认为,如果代码丑陋、令人困惑或难以理解,那么它一定是低效的。
许多人还认为,如果想让代码运行得更快,就必须对其进行丑化。
我见过一些丑陋、令人困惑的代码,其中一些运行得非常快,而另一些则存在巨大的性能问题。
我也看到过干净、清晰、漂亮的代码,同样可以这么说。
我的经验——速度和美是独立的。
It is very common to assume that if code is ugly, confusing, or hard to follow, it must therefore be inefficient.
Many people also think that if you want to make code go faster, you have to uglify it.
I've seen way ugly confusing code, some of which ran very fast, and other of which had massive performance problems.
I've also seen clean, clear, beautiful code of which the same could be said.
My experience - speed and beauty are independent.