newbie question structure of function

Discussion in 'Python' started by menosaint@gmail.com, Mar 13, 2008.

  1. Guest

    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
     
    , Mar 13, 2008
    #1
    1. Advertising

  2. Guest

    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'.
     
    , Mar 13, 2008
    #2
    1. Advertising

  3. Guest

    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
     
    , Mar 14, 2008
    #3
  4. nodrogbrown Guest


    > > > 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
     
    nodrogbrown, Mar 14, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    759
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,231
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    611
  4. Replies:
    9
    Views:
    25,322
    Lal Bahadur Singh
    Nov 11, 2011
  5. meisterbartsch
    Replies:
    2
    Views:
    788
    meisterbartsch
    Jun 12, 2007
Loading...

Share This Page