mod, modulo and % under 2.4 and 2.5

W

W. eWatson

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?
 
S

Steven D'Aprano

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.Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mod' is not defined
 
J

Jan Kaliszewski

01-01-2010 o 02:30:20 W. eWatson said:
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
 
W

W. eWatson

Steven said:
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.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
 
M

MRAB

W. eWatson said:
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! :)
 
M

MRAB

W. eWatson said:
Steven said:
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.
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'.
<ufunc 'remainder'>

Does 'Numeric'?
 
S

Steven D'Aprano

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

Not quite:
'math'

But this only works with functions and classes, not arbitrary objects:
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*
 
W

W. eWatson

Ben said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top