| | |
General syntax questions.
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
What this does is call each iteration of the reverse function each time the "for" loop executes in the statement
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!
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)
def reverse(data): # start at the last index and step back one each time. for index in range(len(data)-1, -1, -1): yield data[index] for char in reverse('golf'): print char """result: f l o g """
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
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
The point is so that you can keep whatever changes you've made to a value. Like:
If I just said
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!
python Syntax (Toggle Plain Text)
# double-up a string def doubleUp(s): return s + s a = "Hello world!" a = doubleUp(a) b = doubleUp(a) print a # should display Hello World!Hello World! print b # should display Hello World!Hello World!Hello World!Hello World!
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)
def doubleUp(s): s = s + s a = "Hello World!" doubleUp(a) print a # should display Hello World!
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.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
![]() |
Similar Threads
- Writing XML parser - some syntax questions (XML, XSLT and XPATH)
- The Official 'College/Degree Questions' Thread (IT Professionals' Lounge)
- more syntax questions (Java)
- Boolean Algebra (C++)
- CSS syntax and commands (JavaScript / DHTML / AJAX)
- Some general assembly questions (Assembly)
- cannot find shell.dll (Windows NT / 2000 / XP)
- Need Help counting Array Length (C++)
- problem about statement (C++)
Other Threads in the Python Forum
- Previous Thread: spell checker advance
- Next Thread: python cgi
Views: 249 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Python
anti array avogadro beginner builtin clear client code color count csv curved def dictionary dynamic enter examples excel file float format frange ftp function gui heads hints homework import input java lapse line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random raw_input recursion recursive redirect script scrolledtext singleton software sqlite ssh stderr string strings subprocess sum syntax table terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wx.wizard wxpython





