Less than Or Greater Than Comparison as a Variable in Python -
I have a block of code in the function, which compares something, that is:
< Code> if customer_info ['total_dls'] & lt; Min_training_actions \ or customer_info ['percent'] & gt; Train_perc_cutoff: test_set \ or test_set [customer_id] ['total_dls'] & lt; Min_testing_actions: num_uncertain + = 1 elif test_set [customer_id] ['percent'] & lt; = Test_perc_cutoff: num_correct + = 1 other: num_incorrect = = 1
Continue Again, sometimes I compared them to more than that comparison, and sometimes I compared them to All other code is exactly the same. Now, I can only create two functions that basically reuse the same code, but before I do, the comparative operator has a clean way to create variables, so I only have one block code Can I use and pass as a variable in comparison? Something like this: Compare (var1, var2, polarity)
. I know that I can make it myself, but I'm wondering what is the standard in such cases. Are there some dragon ways of doing this? I
[emphasize] emphasizing the most important part of the question [/ edit]
You can use; Works in the form of a comparison operator:
if the import operator foobar: comp = operators.lt else: comp = operators.gt if comp (spam, eggs): < / Code>
This will either use the test if spam
is low, or else the egg
, Fobor
on the basis of .
This corresponds to your a variable requirement.
This is definitely used to avoid repeating myself; If you have two pieces of code that are different in only in the comparative direction, then use a function to make the parameter parameter operators
Comments
Post a Comment