2

Say I have two variables:

foo1 = 2
foo2 = x # Actual value unknown

foo1 will always be the same (2) but foo2 can take any integer value from x.

Now say I want to compare foo1 to foo2 (find out if it is smaller, bigger or the same size). I could use == to test if they are the same value:

foo1 == foo2 # False

So foo1 does not equal foo2.

Next I see if foo1 is larger with >:

foo1 > foo2 # False 

So we now know that foo1 is smaller than foo2.

This can be put into a convenient function to compare values:

def cmp(arg1, arg2):

    if arg1 == arg2:
        return 0
    elif arg1 > arg2:
        return 1
    elif arg1 < arg2:
        return -1


print(cmp(2, 3))

Where:

  • 0 means the same size
  • 1 means greater than
  • -1 means smaller than

Interestingly Perl has this inbuilt, the <=> (compareson) operator:

my $foo1 = 2;
my $foo2 = $x;

$foo1 <=> $foo2; 

1 if $foo1 is greater than $foo2

0 if $foo1 and $foo2 are equal

-1 if $foo1 is lower than $foo2

This does the same as the Python function I inplamented above.

Please note that I am not really a Perl coder, but have included this to show that other languages have a compareson feature.


Creating a function is all very well and works fine, however is there some (similar to what Perl uses) inbuilt function/operator that would compare these two values for me (rather than needing to build it myself)?

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • 3
    Python 2 has [`cmp`](https://docs.python.org/2/library/functions.html#cmp). We got rid of it in python 3 for very [good reasons.](https://stackoverflow.com/a/20202496/4819449) – FHTMitchell Jul 06 '18 at 13:31
  • 1
    FYI several other languages, such as PHP, Ruby and C++20, have this ["spaceship operator"](https://en.wikipedia.org/wiki/Three-way_comparison). – scrpy Jul 06 '18 at 13:32
  • 4
    Python 3 docs have [an entire article on this subject](http://portingguide.readthedocs.io/en/latest/comparisons.html) which you might find useful – Green Cloak Guy Jul 06 '18 at 13:33
  • 5
    I honestly can't see why you'd want `cmp` over explicit comparison operators though. Surely you'll need to test the result of `cmp` to do anything useful with it anyway. If it's absolutely neccessary, use `cmp = lambda x, y: (x > y) - (x < y)` – FHTMitchell Jul 06 '18 at 13:34
  • @FHTMitchell Minimising code mainly. Why 4 lines when I could use 1 ;) That lambda function does exactly as I need and is short enough. Thank you – Xantium Jul 06 '18 at 13:39
  • @a625993 Thank you, that article is extremely useful. I have almost finished reading it. – Xantium Jul 06 '18 at 13:41
  • 3
    A) That's such a perl thing to say. Minimising code is not the most important thing here. The comparison operators are more efficient and more readable. B) It doesn't even do that. I genuinely can't think of a situation where that would be the case. Could you maybe provide one? – FHTMitchell Jul 06 '18 at 13:41
  • @FHTMitchell So your advice is to compare them individually. Fair enough. – Xantium Jul 06 '18 at 13:46
  • @FHTMitchell Not really got a specific problem in mind. It's more curiosity that brought me here – Xantium Jul 06 '18 at 13:50
  • 1
    @Simon fair enough :) – FHTMitchell Jul 06 '18 at 13:55

0 Answers0