943,715 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 403
  • Python RSS
Jun 29th, 2009
0

General syntax questions.

Expand Post »
This will hopefully be my last noobish question post.
Please could somebody explain or link to explanation:
1.yeild
2.return
thanx in advance
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Your_mum is offline Offline
30 posts
since Jun 2009
Jun 29th, 2009
1

Re: General syntax questions.

Yield is for generator functions. Return ends the function and sends back the specified value.
Generators are functions that return iterators, i.e. they 'yield' a value after each iteration of them, before proceeding to the next one. This is generally used for things where you don't need all the results returned at once (which must load them all into memory), but instead only gives back the results one at a time (which can vastly reduce memory usage).

Python documentation on generators,
And on iterators.

Here's their example of a simple generator:
python Syntax (Toggle Plain Text)
  1. def reverse(data):
  2. # start at the last index and step back one each time.
  3. for index in range(len(data)-1, -1, -1):
  4. yield data[index]
  5.  
  6. for char in reverse('golf'):
  7. print char
  8.  
  9. """result:
  10. f
  11. l
  12. o
  13. g
  14. """

What this does is call each iteration of the reverse function each time the "for" loop executes in the statement for char in reverse('golf') . This way, only a single letter is sent back each time, rather than the fully-reversed string.
This isn't a great example of generator usage, but say if you had a dataset with 1000s of numbers and you needed to perform calculations on them, but you'd only be working on one item in the set at a time anyway. Then a generator would be useful as it would save a TON of memory by only returning each item as it's needed.
Hope that helped!
Last edited by shadwickman; Jun 29th, 2009 at 4:12 am. Reason: clarity
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jun 29th, 2009
0

Re: General syntax questions.

Thanks, that really helped me. But what is the point of 'returning' a value instead of just leaving it or printing it.
Reputation Points: 10
Solved Threads: 0
Light Poster
Your_mum is offline Offline
30 posts
since Jun 2009
Jun 29th, 2009
0

Re: General syntax questions.

The point is so that you can keep whatever changes you've made to a value. Like:
python Syntax (Toggle Plain Text)
  1. # double-up a string
  2. def doubleUp(s):
  3. return s + s
  4.  
  5. a = "Hello world!"
  6. a = doubleUp(a)
  7. b = doubleUp(a)
  8. print a # should display Hello World!Hello World!
  9. print b # should display Hello World!Hello World!Hello World!Hello World!
If I just said s = s + s in the function, 'a' wouldn't be changed by saying doubleUp(a) as 's' is a variable in the local scope of the function. If I did this:
python Syntax (Toggle Plain Text)
  1. def doubleUp(s):
  2. s = s + s
  3.  
  4. a = "Hello World!"
  5. doubleUp(a)
  6. print a # should display Hello World!
As you can see in the second example, all we did was change the value of 's' inside the function, but a isn't changed because we did not return the value of 's' and assign it back to 'a'.
In C++ you could pass either by pointer or by value, but Python's way of passing is a bit more complicated...
Hope I clarified that!
Last edited by shadwickman; Jun 29th, 2009 at 4:17 pm.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: spell checker advance
Next Thread in Python Forum Timeline: python cgi





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC