passing arguments to a function - do I need type ?

Philipp H. Mohr phm4 at kent.ac.uk
Sun Jul 10 06:19:31 EDT 2005


Hello,
I got a newbie question, I have written the following distance function:

def distance(self,element1, element2):
        dist = 0

        for n in range(len(element1)):
            dist = dist + pow((element1[n] - element2[n]),2)
        print 'dist' + dist
        return sqrt(dist)


and in order to be able to use len() and index element1[] the function
needs to know that the arguments element1 and element2 are both listst or
doesn't it ?

I get the following error msg:

Traceback (most recent call last):
  File "Memory.py", line 105, in ?
    start.inputVector(inP2)
  File "Memory.py", line 97, in inputVector
    allDimensions[0].newAttribute(vector)
  File "Memory.py", line 56, in newAttribute
    dist = self.distance(n.getCenter,newElement)
  File "Memory.py", line 75, in distance
    for n in range(len(element1)):
TypeError: len() of unsized object



AND if I take len out I get:



Traceback (most recent call last):
  File "Memory.py", line 105, in ?
    start.inputVector(inP2)
  File "Memory.py", line 97, in inputVector
    allDimensions[0].newAttribute(vector)
  File "Memory.py", line 56, in newAttribute
    dist = self.distance(n.getCenter,newElement)
  File "Memory.py", line 76, in distance
    dist = dist + pow((element1[n] - element2[n]),2)
TypeError: unsubscriptable object



Thank you very much for your help.

Phil



More information about the Python-list mailing list