[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

newbie question structure of function

menosaint

3/13/2008 10:33:00 PM

hi
I am new to python programming..I would like to call a function that
returns an integer value and a filename string as a tuple.I coded it
like this below...I want to know if this can be coded more compactly
and efficiently..(i am from java background and thus code often
becomes bulky ..)

def mycallerfunction():
matchvalue,matchfilename=findmatchingfile()
if not matchfilename:
print "no match found"
dosomething()

else:
print "match found:",matchfilename,"with matching
distance:",matchvalue
dosomethingelse()


def findmatchingfile():
# calculate matchdistance and matchfilename and return as tuple
# if matchdistance found is not within a threshold then filename
# may be "" (an empty string)
...
...
resultname="""
matchdistance,index=dosomecalculations()
if (matchdistance < threshold):
resultname=filenameslist[index]
return (matchdistance,resultname)


if you can give some advice/suggestions it wd be great

thankx
-vincent
3 Answers

Aaron Brady

3/13/2008 11:14:00 PM

0

Just some FYI options.

>     if not matchfilename:
May return positive if you someday return an object that holds a 'not'
value.

>     resultname="""
That starts a string literal.

> if (matchdistance < threshold):
Parentheses optional.

>     return (matchdistance,resultname)
Parentheses optional there too. "return matchdistance," returns a
'one-tuple'.

menosaint

3/14/2008 5:45:00 AM

0

Aaron
thanx for the input


> > resultname="""
> > That starts a string literal.

i am not sure if this is the right way..i used it in case
matchdistance < threshold return False.then the filename will not be
taken from the filenameslist and so returns as an empty string.



> > return (matchdistance,resultname)
>
> Parentheses optional there too. "return matchdistance," returns a
> 'one-tuple'.

but then the call matchvalue,matchfilename=findmatchingfile()
will give ValueError trying to unpack from a one-tuple

if anyone can suggest a more decent way of doing this pls do
thanx
vincent

nodrogbrown

3/14/2008 6:42:00 AM

0


> > > resultname="""
> > > That starts a string literal.
>
> i am not sure if this is the right way..i used it in case
> matchdistance < threshold return False.then the filename will not be
> taken from the filenameslist and so returns as an empty string.
>a one-tuple


why not use None instead?

if (matchdistance < threshold):
resultname=filenameslist[index]
else:
resultname=None

will that not be better?
gordon