用许多 if 子句总结 python 中的代码 [英] summarise code in python with many if-clauses

查看:29
本文介绍了用许多 if 子句总结 python 中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前为此使用了大量代码.有没有办法总结代码?非常感谢!

I am currently using a lot of code for this. Is there a way to summarise the code? Thanks a lot!

我想总结一下 If 条件.

I would like to summarise the If conditions.

'''

if self.calculations["A1"].get() == 0 and event_measure == "A1":
    self.Menu_blade_exchange["A1"].grid(row =2, column = 1, padx=3, pady=1, sticky = "W")
    self.Menu_rubbing_marks["A1"].grid(row =2, column = 2, padx=3, pady=1, sticky = "W")

if self.calculations["A1"].get() == 1 and event_measure == "A1":
    self.Menu_blade_exchange["A1"].grid_forget()
    self.Menu_rubbing_marks["A1"].grid_forget()
    self.Menu_rubbing_marks_border["A1"].grid_forget()
    self.blade_exchange_Type["A1"].set('')
    self.rubbing_marks_Type["A1"].set('')
    self.rubbing_marks_border_Type["A1"].set('')

if self.calculations["A2"].get() == 0 and event_measure == "A2":
    self.Menu_blade_exchange["A2"].grid(row =3, column = 1, padx=3, pady=1, sticky = "W")
    self.Menu_rubbing_marks["A2"].grid(row =3, column = 2, padx=3, pady=1, sticky = "W")

if self.calculations["A2"].get() == 1 and event_measure == "A2":
    self.Menu_blade_exchange["A2"].grid_forget()
    self.Menu_rubbing_marks["A2"].grid_forget()
    self.Menu_rubbing_marks_border["A2"].grid_forget()
    self.blade_exchange_Type["A2"].set('')
    self.rubbing_marks_Type["A2"].set('')
    self.rubbing_marks_border_Type["A2"].set('')

....'''

推荐答案

由于您没有提供完整的示例,因此很难理解您要完成的任务.如果我们可以假设每个代码块都是相同的,并且唯一改变的是数组的索引,那么您也许可以用一个代码块替换所有 if 语句:

It's a bit hard to understand what you are trying to accomplish since you didn't provide a complete example. If we can assume that each block of code is identical and the only thing that is changing is the index into the array, you might be able to replace all of the if statements with a single block of code:

if self.calculations[event_measure].get() == 0:
    self.Menu_blade_exchange[event_measure].grid(row =2, column = 1, padx=3, pady=1, sticky = "W")
    self.Menu_rubbing_marks[event_measure].grid(row =2, column = 2, padx=3, pady=1, sticky = "W")

else:
    self.Menu_blade_exchange[event_measure].grid_forget()
    self.Menu_rubbing_marks[event_measure].grid_forget()
    self.Menu_rubbing_marks_border[event_measure].grid_forget()
    self.blade_exchange_Type[event_measure].set('')
    self.rubbing_marks_Type[event_measure].set('')
    self.rubbing_marks_border_Type[event_measure].set('')

在评论中有人指出行号发生了变化,这也可以通过将行号保存在数组中来解决

In the comments someone pointed out that the row numbers change, which could be solved by saving the row numbers in an array as well

rownum = {"A1": 2, "A2": 3, ...}
...
if self.calculations[event_measure].get() == 0:
    self.Menu_blade_exchange[event_measure].grid(row =rownum[event_measure], column = 1, padx=3, pady=1, sticky = "W")
    self.Menu_rubbing_marks[event_measure].grid(row =rownum[event_measure], column = 2, padx=3, pady=1, sticky = "W")

另一种解决方案可能是使用 grid_remove 而不是 grid_forget,以便记住所有配置选项.

Another solution might be to use grid_remove rather than grid_forget, so that all of the configuration options are remembered.

如果每个事件度量"都可以,这可能会容易得多.是一个类的实例.

This would likely be much, much easier if each "event measure" was an instance of a class.

这篇关于用许多 if 子句总结 python 中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆