A new to Python question

Philippe C. Martin philippe at philippecmartin.com
Sat May 14 13:13:07 EDT 2005


Hi,

You're thinking you're passing the arguments as reference (look at mutable
vs non-mutable)

Your function returns the values in a tupple (x,y,...); you need to fetch
the values from that tupple

Regards,

Philippe





David wrote:

> Hi I'm trying to teach myself python and so far to good, but I'm having
> a bit of trouble getting a function to work the way I think it should
> work.  Right now I'm taking a simple program I wrote in Fortran and
> trying to do it in Python.  I got it to work, but now I'm trying to
> modularize it.  My fortran program uses a subroutine and I was trying
> to do the same thing in Python.  But I'm still new so I'm having
> trouble understanding what I'm doing wrong.  Here is my code:
> 
> #! /usr/bin/python
> #This program takes two vectors and multiplies them
> #against a 10X10 array.  It also then gives the dot product,
> #sum, and max value of the array.
> 
> import Numeric
> def abc(array1,array2,dotprod,sum,maxvalue):
>     """Takes two arrays and performs predetermined calculations,
>        Then returns the solutions back as the same array, along
>        with the dot product, sum, and max value of the array."""
>     #TODO: Get this damn thing working!!
>     print "Array 1 in:",array1 #Debug
>     data = Numeric.zeros((10,10))
>     for i in range(10):
>         for j in range(10):
>             data[i,j] = (i+1)+(j+1)
> 
>     e = Numeric.matrixmultiply(data, array1)
>     g = Numeric.matrixmultiply(Numeric.transpose(data),array2)
>     array1 = e
>     array2 = g
>     dotprod = Numeric.dot(array1,array2)
>     sum = Numeric.sum(array1)
>     maxvalue = array2[Numeric.argmax(array2)]
>     print "Array 1 out:",array1 #Debug
> 
>     return array1,array2,dotprod,sum,maxvalue #<<-- Not working as I
> thought it would.
> 
> x = Numeric.arange(1,11)
> y = Numeric.arange(1,11)*2
> dotp,sumx,maxv = 0,0,0  #Is this the only way to declare a variable?
> 
> print 'Array X:',x
> print 'Array Y:',y
> abc(x,y,dotp,sumx,maxv)
> print 'Calling function abc'
> print 'Array X:',x
> print 'Array Y:',y
> print 'Dot Product of X and Y:',dotp
> print 'Sum of array X:',sumx
> print 'Max value of array Y:',maxv
> 
> If you run it the data gets passed to the function just fine and it
> finds the right numbers.  Its just getting it to pass them back thats
> not working.  I put some print statements inside the function just to
> see how the data gets passed.  So any ideas that would help me? If you
> want to see the fortran code just email me.
> 
> David




More information about the Python-list mailing list