[英]Is there a way to achieve a sort of placeholder for Python object attributes?
Hoping someone can help me with just pseudocode.希望有人能用伪代码帮助我。 I can't copy the code here as it's not entirely my own.
我不能在这里复制代码,因为它不完全是我自己的。
I have a function that looks like this:我有一个看起来像这样的函数:
for result in results_set:
if conditionA:
# When conditionA is true, test on this_attribute.
if result.this_attribute == "interesting string":
# do things.
if result.this_attribute == "another interesting string"
# do different things.
else:
# ConditionA is false? Test on that_other_attribute instead.
if result.that_other_attribute == "interesting string"
# do the same exact things as above for "interesting string"
if result.that_other_attribute == "another interesting string"
# do the same exact things as above for "another interesting string"
It seems very inefficient to have the test for conditionA or conditionB be inside the for loop since I deal with results_sets that can be thousands of lines long.将 conditionA 或 conditionB 的测试放在for 循环内似乎非常低效,因为我处理的结果集可能长达数千行。 Plus the code looks bad because I'm just repeating myself.
另外代码看起来很糟糕,因为我只是在重复自己。
Feels like I should be able to test for conditionA / B before the loop takes place, and tell Python what attribute of "result" to compare next based on that test.感觉我应该能够在循环发生之前测试条件 A / B,并告诉 Python 下一个基于该测试比较“结果”的属性。
Which attribute I test will always depend on the value of ConditionA.我测试哪个属性将始终取决于 ConditionA 的值。 I may end up with a ConditionB, C or D in the near future as well which will require checking on a third, fourth or fifth attribute of result .
我可能会在不久的将来得到 ConditionB、C 或 D,这将需要检查result的第三、第四或第五个属性。
Currently I solved this by having two nearly identical functions that each have their own "for" without the ConditionA test inside it... but that looks bad and will become a nightmare when B, C or D roll around.目前,我通过使用两个几乎相同的函数解决了这个问题,每个函数都有自己的“for”,其中没有 ConditionA 测试……但这看起来很糟糕,当 B、C 或 D 出现时,这将成为一场噩梦。
Is it possible to have an attribute placeholder somehow?是否有可能以某种方式拥有属性占位符? If so, how please?
如果是这样,请问如何?
Edit:编辑:
I am trying to achieve something like this....我正在努力实现这样的目标......
result = a filler value used only to reference attribute names
if ConditionA:
check_attribute = result.this_attribute
else:
check_attribute = result.that_other_attribute
for result in results_set:
if check_attribute == "interesting string":
# do things.
if check_attribute == "another interesting string"
# do different things.
Using getattrs might get you somewhere here, however unlikely that may sound.使用 getattrs 可能会让你到达某个地方,但这听起来不太可能。 Right above the for loop, you could do
在 for 循环的正上方,你可以做
value_to_check = "this_attribute" if conditionA else "that_other_attribute".
Yes, those were strings .是的,那些是字符串。 Next, in the
for
loop, you could do接下来,在
for
循环中,你可以做
result_value = getattr (result, value_to_check)
if result_value == "interesting string": #thing to do
elif result_value == "another interesting string": #other thing to do
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.