python风格:不需要内联的内联函数?
我正在写gtk代码。我经常有不需要闭包的简短回调,因为它们传递了所需的所有参数。例如,我在创建一些 gtk.TreeViewColumns
时将其置于循环中:
def widthChanged(MAINCOL, SPEC, SUBCOL, expandable):
if expandable: return
w = MAINCOL.get_width()
SUBCOL.set_fixed_width(w)
cl.connect("notify::width", widthChanged, pnlcl, expand)
这可能效率低下,因为该函数是在循环的每次迭代中创建的(附带问题:它实际上是,还是优化了吗?)。然而,我觉得如果我把所有这些单行文字移到顶层,代码会更加混乱。有什么意见吗?
I'mm writing gtk code. I often have short callbacks that don't need to be closures, as they are passed all the parameters they need. For example, I have this in a loop when creating some gtk.TreeViewColumns
:
def widthChanged(MAINCOL, SPEC, SUBCOL, expandable):
if expandable: return
w = MAINCOL.get_width()
SUBCOL.set_fixed_width(w)
cl.connect("notify::width", widthChanged, pnlcl, expand)
This is probably inefficient, since the function is being created on every iteration of the loop (side-question: is it actually, or is it optimized?). However, I feel like if I moved all these one-liners to the top level, the code would be more confusing. Any opinions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
采用最具可读性的风格。不要担心速度,除非您的代码分析工具告诉您该区域是热点。
Go with whatever style is most readable. Don't worry about speed unless your code profiling tools have told you that the area is a hotspot.