[Edu-sig] Typed arrays (not about div)

Kirby Urner urnerk@qwest.net
Fri, 11 Oct 2002 18:47:51 -0700


At 08:34 PM 10/11/2002 -0400, Arthur wrote:
> >But I have the ability to foresee where Bruce's and Michael's students are
> >most likely to get caught with *their* pants down, under the new operator.
> >Which are ways even more subtle, IMO, but just as likely to arise as with
>my
> >raised to a power issue.  So, on the gotcha score - at best a wash, IMO.
>
>Kirby writes -
>
> >(You don't spell out the gotchas with the new behavior -- examples
> >might have been nice.)
>
>Gotchas with the new behavior are not directly gotchas with the new
>behavior.  They are gotchas that arise when being blindsided by the fact
>that numeric typing matters.  Something clear to anyone functioning under
>the old system.
>
>The one I raised to Michael was:
>
> >>> fromNumeric import *
> >>> a=array([1,2,3])
> >>> a[1]=.5
> >>> a
>array([1, 0, 3])
>
>Huh? under new "/"
>ouch - I should have seen that coming under old "/"

OK, now look at this:

  >>> from Numeric import *
  >>> b = array([1,2,3])
  >>> b[1] = 'a'
  >>> b
  array([ 1, 97,  3])

Same issue, yet very clearly this has nothing whatever to do with
the behavior of div, either way.  We're facing the fact that Numeric
doesn't allow mixing types inside an array type.  To bring div into
it only confuses the issue. 'a' is being coerced to an int:

   >>> ord('a')
   97

Note that Numeric is extrinsic to core Python and has a number
of dialectical wrinkles.  Closer to home would have been to use
the array module itself, which takes the type as a first
argument, thereby clarifying what's going on:

  >>> from array import *
  >>> b = array([1,2,3])     # can't get away without declaring type
  Traceback (most recent call last):
    File "<pyshell#1>", line 1, in ?
      b = array([1,2,3])
  TypeError: array() takes exactly 2 arguments (1 given)
  >>> b = array('i',[1,2,3])
  >>> b
  array('i', [1, 2, 3])
  >>> b[1] = 'a'             # doesn't coerce  (unlike Numeric)
  Traceback (most recent call last):
    File "<pyshell#4>", line 1, in ?
      b[1] = 'a'
  TypeError: an integer is required
  >>> b[1] = 2.
  >>> b                  # ah, so there's automatic coercion of floats
  array('i', [1, 2, 3])
  >>> b[1] = 2.1         # ... akin to going int(2.1)
  >>> b
  array('i', [1, 2, 3])

>Michael acknowledged that that exact situation had already arisen and thrown
>folks for a loop in his class.

No doubt.  Linking this to the behavior of div (which doesn't change
either way) would have only confused them more.

>And *I'm* the English major.
>
>Art

Any serious study of programming qua programming (philosophy major)
involves studying types,. And, as Danny Yoo pointed out above, you
*don't* want to use Python as your primary vehicle for studying types.

Any programming course that just looks at one and only one computer
language isn't really a programming course, the way I see it.  It's
fine to *concentrate* on one language, but you need others in the
picture to compare and contast.

The argument can be made that "mono-lingual computer programmer"
is an oxymoron (as an English major, you no doubt grok).

Kirby