#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11

    Returning multiple values from a function


    Hi

    I have not posted here along time, so ...

    But as said above how do I return more then one value for example when I iterate over a list how can I return every object seperate.

    Or can i not od that and do i need to return a list?
    Those people who think they know everything are a great annoyance to those of us who do.
  2. #2
  3. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    619
    Is this what you're looking for?
    Code:
    #! /usr/bin/env python
    
    def getdata():
    	return 12, 42
    
    i, j = getdata()
    
    print i, j
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    ya but i meant like more then one return statement...

    since return quits the function?
    Those people who think they know everything are a great annoyance to those of us who do.
  6. #4
  7. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    619
    Do you mean like this?
    Code:
    #! /usr/bin/env python
    
    def getdata(number):
    	if number % 2 == 0:
    		return 12
    	else:
    		return 42
    
    i = getdata(14)
    
    print i
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    I think you want a generator. Generators are like functions but by using the 'yield' keyword instead of return you can iterate over it like a list and each call will carry on from where the previous one left off.

    e.g.

    python Code:
    >>> def fibbonnaci(max):
    ...      a = b = 1
    ...      while a < max:
    ...         yield a
    ...         a, b = b, a+b
    ...         
     
    >>> for x in fibbonnaci(100): print x
    ...  
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89


    Dave

    Comments on this post

    • Yegg` agrees
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    yes i think that is something to look at:P thnaks
    Those people who think they know everything are a great annoyance to those of us who do.

IMN logo majestic logo threadwatch logo seochat tools logo