A new to Python question

M.E.Farmer mefjr75 at hotmail.com
Sat May 14 13:44:30 EDT 2005


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

Hello David,
   welcome to Python!
Python always returns 'something' from a function or method if nothing
is specified or there is no return you get an implicit None.
You have created a function that accepts a few arguments and processes
them then returns there value.
The problem is that you **have not** used the return values ;)
example:

def funk(first,second,third):
    return first,second,third

This can be like this:
print funk("theses three args can be anything",2,"three")

If you need to use the return values you need to assign them to a
"name" so you have a handle on them.
Variables are don't exist in Python there are only objects and names(
hence namespaces ), but not everyone is so strict and you still see
mention of 'variables' when they mean 'names'.
> ##dotp,sumx,maxv = 0,0,0  # not needed here
> print 'Array X:',x
> print 'Array Y:',y
> ## Notice we assign the return values to names so we can access it
later
> arr1,arr2,dotp,sumx,maxv = abc(x,y,0,0,0)
> print 'Calling function abc'
> print 'Array X:',arr1
> print 'Array Y:',arr2
> print 'Dot Product of X and Y:',dotp
> print 'Sum of array X:',sumx
> print 'Max value of array Y:',maxv

Or you could pack all return values into a tuple and access it thru
slices.
> ##dotp,sumx,maxv = 0,0,0  # not needed here
> print 'Array X:',x
> print 'Array Y:',y
> abcinfo = abc(x,y,0,0,0)
> print 'Calling function abc'
> print 'Array X:',abcinfo[0]
> print 'Array Y:',abcinfo[1]
> print 'Dot Product of X and Y:',abcinfo[2]
> print 'Sum of array X:',abcinfo[3]
> print 'Max value of array Y:',abcinfo[4]

Or you could use a dictionary, or etc...
The possibilities are endless.
Be sure to study up on namespaces, it will ease your Python woes.
Namespaces are the most fundamental part of Python that new users don't
understand. Namespace mastery will take you far.
Just remember there are only two scopes local and global ;)
http://docs.python.org
hth,
M.E.Farmer




More information about the Python-list mailing list