O'Reilly logo

Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython by Richard Hightower

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Operations

This section deals mostly with functions that are longhand for operators.

Numeric Operations: cmp(), divmod(), pow(), abs(), round()

cmp(x,y) compares two numeric types. It also works for sequences, dictionaries, and other numeric objects. cmp() returns a positive number if the x argument is greater than the y argument, a negative number if the y argument is greater than the x argument, and 0 if x and y are equal.

>>> cmp(1,0)
1

>>> cmp(0,1)
-1

>>> cmp(0,0)
0

cmp() is related to the equality operators (==, >=, <=, etc.). If you want these operators to work with class instances, you need to implement cmp() for the class by executing cmp(instance1, instance2), instance1 == instance2, or instance1 > instance2).

The divmod() function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required