Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mod, modulo and % under 2.4 and 2.5

1 view
Skip to first unread message

W. eWatson

unread,
Dec 31, 2009, 8:30:20 PM12/31/09
to
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?

Steven D'Aprano

unread,
Dec 31, 2009, 8:51:15 PM12/31/09
to

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

Jan Kaliszewski

unread,
Dec 31, 2009, 9:04:08 PM12/31/09
to pytho...@python.org

???

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

W. eWatson

unread,
Dec 31, 2009, 9:06:25 PM12/31/09
to
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

MRAB

unread,
Dec 31, 2009, 9:15:33 PM12/31/09
to pytho...@python.org

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

unread,
Dec 31, 2009, 9:24:08 PM12/31/09
to pytho...@python.org
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'?

Ben Finney

unread,
Dec 31, 2009, 9:48:28 PM12/31/09
to
"W. eWatson" <wolft...@invalid.com> 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.

--
\ “Generally speaking, the errors in religion are dangerous; |
`\ those in philosophy only ridiculous.” —David Hume, _A Treatise |
_o__) of Human Nature_, 1739 |
Ben Finney

Steven D'Aprano

unread,
Dec 31, 2009, 10:06:20 PM12/31/09
to
On Fri, 01 Jan 2010 13:48:28 +1100, Ben Finney wrote:

> "W. eWatson" <wolft...@invalid.com> 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

W. eWatson

unread,
Jan 1, 2010, 12:49:28 AM1/1/10
to
Ben Finney wrote:
> "W. eWatson" <wolft...@invalid.com> 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!
0 new messages