mod, modulo and % under 2.4 and 2.5

Discussion in 'Python' started by W. eWatson, Jan 1, 2010.

  1. W. eWatson

    W. eWatson Guest

    About a year ago, I wrote a program that used mod() for modulo under
    2.5. Apparently, % is also acceptable, but the program works quite well.
    I turned the program over to someone who is using 2.4, and apparently
    2.4 knows nothing about mod(). Out of curiosity, what library is
    mod(a,b)(two args) in? It doesn't seem to be in numpy. It seems to be
    built-in. If so, why isn't it both 2.4 and 2.5?
    W. eWatson, Jan 1, 2010
    #1
    1. Advertising

  2. On Thu, 31 Dec 2009 17:30:20 -0800, W. eWatson wrote:

    > About a year ago, I wrote a program that used mod() for modulo under
    > 2.5. Apparently, % is also acceptable, but the program works quite well.
    > I turned the program over to someone who is using 2.4, and apparently
    > 2.4 knows nothing about mod(). Out of curiosity, what library is
    > mod(a,b)(two args) in? It doesn't seem to be in numpy. It seems to be
    > built-in.


    No it doesn't.

    [steve@sylar ~]$ python2.5
    Python 2.5 (r25:51908, Nov 6 2007, 16:54:01)
    [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> mod

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'mod' is not defined


    --
    Steven
    Steven D'Aprano, Jan 1, 2010
    #2
    1. Advertising

  3. 01-01-2010 o 02:30:20 W. eWatson <> wrote:

    > About a year ago, I wrote a program that used mod() for modulo under
    > 2.5. Apparently, % is also acceptable, but the program works quite well.
    > I turned the program over to someone who is using 2.4, and apparently
    > 2.4 knows nothing about mod(). Out of curiosity, what library is
    > mod(a,b)(two args) in? It doesn't seem to be in numpy. It seems to be
    > built-in. If so, why isn't it both 2.4 and 2.5?


    ???

    There is no builtin mod() function at all, but there are (in Py 2.4, 2.5,
    2.6, 3.0 and 3.1):
    * builtin '%' and '%=' operators
    * builtin divmod()
    * in 'operator' module: mod() or __mod__() [the same] -- equivalents of
    '%' operator
    * in 'math' module: fmod() function

    Additionaly, since Py 2.5 in 'operator' module there is imod() and
    __imod__() [the same] -- equivalents of '%=' operator.

    Cheers,
    *j
    Jan Kaliszewski, Jan 1, 2010
    #3
  4. W. eWatson

    W. eWatson Guest

    Steven D'Aprano wrote:
    > On Thu, 31 Dec 2009 17:30:20 -0800, W. eWatson wrote:
    >
    >> About a year ago, I wrote a program that used mod() for modulo under
    >> 2.5. Apparently, % is also acceptable, but the program works quite well.
    >> I turned the program over to someone who is using 2.4, and apparently
    >> 2.4 knows nothing about mod(). Out of curiosity, what library is
    >> mod(a,b)(two args) in? It doesn't seem to be in numpy. It seems to be
    >> built-in.

    >
    > No it doesn't.
    >
    > [steve@sylar ~]$ python2.5
    > Python 2.5 (r25:51908, Nov 6 2007, 16:54:01)
    > [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >>>> mod

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > NameError: name 'mod' is not defined
    >
    >

    So where is it? Here are the choices.
    import sys, os, glob
    import string
    from numpy import *
    from datetime import datetime, timedelta
    import time

    In the 2.4 version, I change nmnpy to Numeric
    W. eWatson, Jan 1, 2010
    #4
  5. W. eWatson

    MRAB Guest

    W. eWatson wrote:
    > About a year ago, I wrote a program that used mod() for modulo under
    > 2.5. Apparently, % is also acceptable, but the program works quite
    > well. I turned the program over to someone who is using 2.4, and
    > apparently 2.4 knows nothing about mod(). Out of curiosity, what
    > library is mod(a,b)(two args) in? It doesn't seem to be in numpy. It
    > seems to be built-in. If so, why isn't it both 2.4 and 2.5?


    mod() not is a built-in.

    It is, however, in the 'operator' module, and also as __mod__() in that
    same module. Both are equivalent to '%'.

    It has been there since at least Python v2.0.

    As for why something might not be in an earlier version, well, that
    would be because it hadn't been added yet! :)
    MRAB, Jan 1, 2010
    #5
  6. W. eWatson

    MRAB Guest

    W. eWatson wrote:
    > Steven D'Aprano wrote:
    >> On Thu, 31 Dec 2009 17:30:20 -0800, W. eWatson wrote:
    >>
    >>> About a year ago, I wrote a program that used mod() for modulo under
    >>> 2.5. Apparently, % is also acceptable, but the program works quite well.
    >>> I turned the program over to someone who is using 2.4, and apparently
    >>> 2.4 knows nothing about mod(). Out of curiosity, what library is
    >>> mod(a,b)(two args) in? It doesn't seem to be in numpy. It seems to be
    >>> built-in.

    >>
    >> No it doesn't.
    >>
    >> [steve@sylar ~]$ python2.5
    >> Python 2.5 (r25:51908, Nov 6 2007, 16:54:01)
    >> [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
    >> Type "help", "copyright", "credits" or "license" for more information.
    >>>>> mod

    >> Traceback (most recent call last):
    >> File "<stdin>", line 1, in <module>
    >> NameError: name 'mod' is not defined
    >>
    >>

    If you think it's built-in then you've probably imported it from a
    module using the form "from some_module import *".

    > So where is it? Here are the choices.
    > import sys, os, glob
    > import string
    > from numpy import *


    Aha, there you are!

    > from datetime import datetime, timedelta
    > import time
    >
    > In the 2.4 version, I change nmnpy to Numeric


    'numpy' does contain a function called 'mod'.

    >>> import numpy
    >>> numpy.mod

    <ufunc 'remainder'>

    Does 'Numeric'?
    MRAB, Jan 1, 2010
    #6
  7. On Fri, 01 Jan 2010 13:48:28 +1100, Ben Finney wrote:

    > "W. eWatson" <> writes:
    >
    >> Steven D'Aprano wrote:
    >> > NameError: name 'mod' is not defined

    >
    >> So where is it? Here are the choices. import sys, os, glob
    >> import string
    >> from numpy import *

    >
    > If you use ‘from foo import *’ you forfeit any way of saying where a
    > name in your code gets bound.


    Not quite:

    >>> from math import *
    >>> sin.__module__

    'math'

    But this only works with functions and classes, not arbitrary objects:

    >>> pi.__module__

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'float' object has no attribute '__module__'


    > Hence, don't do that.


    Avoiding "from module import *" is generally excellent advice. There's
    one or two exceptions, but if you have to ask what they are, you don't
    need to know *wink*



    --
    Steven
    Steven D'Aprano, Jan 1, 2010
    #7
  8. W. eWatson

    W. eWatson Guest

    Ben Finney wrote:
    > "W. eWatson" <> writes:
    >
    >> Steven D'Aprano wrote:
    >>> NameError: name 'mod' is not defined

    >
    >> So where is it? Here are the choices.
    >> import sys, os, glob
    >> import string
    >> from numpy import *

    >
    > If you use ‘from foo import *’ you forfeit any way of saying where a
    > name in your code gets bound.
    >
    > Hence, don't do that.
    >

    Good idea!
    W. eWatson, Jan 1, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Kruggel

    General modulo-question

    Christian Kruggel, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    441
    Brad BARCLAY
    Jul 7, 2003
  2. Guest
    Replies:
    0
    Views:
    3,679
    Guest
    Jan 18, 2004
  3. Hari Sekhon
    Replies:
    0
    Views:
    496
    Hari Sekhon
    Jun 20, 2006
  4. ryles
    Replies:
    3
    Views:
    516
    Piet van Oostrum
    Jul 26, 2009
  5. T. Onoma
    Replies:
    9
    Views:
    341
    Dave Thomas
    Dec 15, 2003
Loading...

Share This Page