The Wayback Machine - https://web.archive.org/web/20151206073853/http://stackoverflow.com/questions/linked/231767

Linked Questions

17
votes
5answers
1k views

for x in y(): how does this work? [duplicate]

I was looking for code to spin a cursor in the terminal and found this. I was wondering what was happening in the code. In particular for c in spinning_cursor(): I've never seen this syntax. Is it ...
6
votes
4answers
2k views

What is the “yield” statement in a Python function? [duplicate]

Possible Duplicate: The Python yield keyword explained Can someone explain to me what the yield statement actually does in this bit of code here: def fibonacci(): a, b = 0, 1 ...
4
votes
4answers
4k views

What does yield do in python 2.7? [duplicate]

Possible Duplicate: The Python yield keyword explained Okay, I've probably phrased the question badly but this is the situation I have. I have this line of code in Python 2.7 which I'm ...
4
votes
3answers
1k views

How does a function in Python remember its values after it returns? [duplicate]

Warning: extreme newbie question I seem to have been thinking of functions as a recipe. In my world, the program is a recipe box and some of the recipes (functions) call for other recipes (other ...
3
votes
1answer
202 views

What do you call a function with a yield [duplicate]

What is the name name for functions that contain yield statements? yield "what am I called" Is there a name for them?
-1
votes
4answers
121 views

What does “for i in generator():” do? [duplicate]

Can someone explain what each step in this does? I have never seen "for i in X:" used where X is a generator, and I am failing to understand how the i interacts with the function if it's not being ...
0
votes
1answer
184 views

Yield does not work, but return does [duplicate]

I'm making a Python irc bot. For some reason the yield statement in my join() method makes it skip the method altogether, but if I replace it with a return it works fine. However, I need to yield an ...
6
votes
1answer
161 views

Trouble understanding python generators [duplicate]

I am new to generator in python. I have a simple enough code which I am playing with but I can not understand the output I am getting out of it. Here is my code : def do_gen(): for i in range(3): ...
1
vote
2answers
140 views

Python yield statement - don't understand this works this way [duplicate]

Why does this code: #!/usr/bin/env python def createGenerator(): mylist = [ 'alpha', 'beta', 'carotene' ] for i in mylist: yield i, "one" yield i, "two" yield i, ...
-2
votes
1answer
140 views

basic understanding about yield [duplicate]

def merge2(iter1,iter2): """ on input iter1, iter2, two non-empty sorted iterators, not necessarily infinite, produces the sorted merge of the two iterators """ left=next(iter1) ...
0
votes
1answer
120 views

what's different about loop data with yield and without yield [duplicate]

I have two ways to loop data, one using with for loop another with yield, I want to know what's the difference. For Loop(get 100000 data) data='select 100000 data from database' for d in date: ...
0
votes
1answer
65 views

Python - yield vs. return [duplicate]

I am trying to understand the yield vs. return functionality in Python 2.7 . I created two similar examples (per below) - and had some questions that might reveal that i still might not fully ...
0
votes
0answers
57 views

Use Of Yield with List In Python Programming? [duplicate]

I am learning python programming and about yield I learnt that for yield we have to learn generators, But please any one can explain me yield in simple words and I have 1 Query that In Following ...
-1
votes
3answers
54 views

Understanding yield in python [duplicate]

I am Python amateur and was trying to write a code to generate a Fibonacci Series when I came across a weird behaviour. >>> def F(num): #line1 a,b = 1,2 ...
-1
votes
1answer
51 views

yield( generator) in Function [duplicate]

i have been test some code in IDLE but i have some problem to understand a code code : def g(): yield 'Stormx' print('hi Stormx') yield 'Gaza' print('free to Gaza') look when i ...
0
votes
0answers
38 views

What does “val = yield xxx” mean in Python [duplicate]

I know what does yield do when we write say while True: yield i i += 1 But what does it mean when we say def foo(): A = yield B ? What would be the value of A then? The reason why I ...
0
votes
1answer
72 views

Why does this code yield a generator? [duplicate]

I wrote the following into the Python interpreter today: >>> def test(): ... for c in ['a', 'b', 'c', 'd']: yield c ... >>> a = test() >>> a <generator object test ...
0
votes
0answers
22 views

python:Difference between Iterables and generator and yield in python? [duplicate]

I came across these terms iterables, generator and yield while solving the following problem which uses the yield keyword: question: Define a class with a generator which can iterate the numbers, ...
0
votes
0answers
21 views

Difference between function and generator? [duplicate]

I am looking for the exact difference between normal function and generator. I have already googled it. But all the results are quite confusing. I am a beginner, so i am expecting some short and ...
1
vote
0answers
21 views

how yield in the following code snippet works on Python [duplicate]

Normally people are using in a loop and tried the following code works and wondering how sum leverage yield? # a generator that yields items instead of returning a list def firstn(n): num = 0 ...
0
votes
0answers
19 views

Understanding Hadoop mappers in Python [duplicate]

I'm refering to this popular blog post: http://www.michael-noll.com/tutorials/writing-an-hadoop-mapreduce-program-in-python/ Here, the author first demonstrates a simple regular Python mapper ...
0
votes
0answers
16 views

How yield is working in below code [duplicate]

I got following combinations function from the python docs for itertools. def combinations(iterable, r): # combinations('ABCD', 2) --> AB AC AD BC BD CD # combinations(range(4), 3) --> ...
0
votes
0answers
12 views

understanding the use of yield in a function that removes duplicates [duplicate]

I'm reading Python Cookbook 3.0 and I encountered this solution to the problem of removing duplicates while maintaining the order of items (assuming the items are hashable): def dedupe(items): ...
1422
votes
191answers
505k views

Hidden features of Python [closed]

What are the lesser-known but useful features of the Python programming language? Try to limit answers to Python core. One feature per answer. Give an example and short description of the feature, ...
538
votes
18answers
256k views

Random string generation with upper case letters and digits in Python

I want to generate a string of size N. It should be made up of numbers and uppercase English letters such as: 6U1S75 4Z4UKK U911K4 How can I achieve this in a pythonic way?
70
votes
10answers
9k views

Advice for C# programmer writing Python [closed]

I've mainly been doing C# development for the past few years but recently started to do a bit of Python (not Iron Python). But I'm not sure if I've made the mental leap to Python...I kind of feel I'm ...
61
votes
4answers
55k views

What's the common practice for enums in Python? [duplicate]

Possible Duplicate: How can I represent an 'enum' in Python? What's the common practice for enums in Python? I.e. how are they replicated in Python? public enum Materials { Shaded, ...
44
votes
7answers
33k views

How to implement __iter__(self) for a container object (Python)

I have written a custom container object. According to this page, I need to implement this method on my object: __iter__(self) However, upon following up the link to Iterator Types in the Python ...
17
votes
8answers
28k views

Explaining the python 'self' variable to a beginner

I'm pretty much ignorant of OOP jargon and concepts. I know conceptually what an object is, and that objects have methods. I even understand that in python, classes are objects! That's cool, I just ...
27
votes
9answers
16k views

Split a list into parts based on a set of indexes in Python

What is the best way to split a list into parts based on an arbitrary number of indexes? E.g. given the code below indexes = [5, 12, 17] list = range(20) return something like this part1 = ...
8
votes
11answers
1k views

Does Python have something like Perl 5.10's “state” variables?

In Perl 5.10, I can say: sub foo () { state $x = 1; say $x++; } foo(); foo(); foo(); ...and it will print out: 1 2 3 Does Python have something like this?
15
votes
10answers
3k views

Python, beyond the basics

I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to ...
22
votes
5answers
2k views

Does enumerate() produce a generator object?

As a complete Python newbie, it certainly looks that way. Running the following... x = enumerate(['fee', 'fie', 'foe']) x.next() # Out[1]: (0, 'fee') list(x) # Out[2]: [(1, 'fie'), (2, 'foe')] ...
9
votes
5answers
62k views

Python return list from function

I have a function that parses a file into a list. I'm trying to return that list so I can use it in other functions. def splitNet(): network = [] for line in ...
10
votes
4answers
12k views

Where to use yield in Python best?

I know how yield works. I know permutation, think it just as a math simplicity. But what's yield's true force? When should I use it? A simple and good example is better.
22
votes
2answers
2k views

what does yield as assignment do? myVar = (yield)

I'm familiar with yield to return a value thanks mostly to this question but what does yield do when it is on the right side of an assignment? @coroutine def protocol(target=None): while True: ...
12
votes
3answers
5k views

Python range() and zip() object type

I understand how functions like range() and zip() can be used in a for loop. However I expected range() to output a list - much like seq in the unix shell. If I run the following code: a=range(10) ...
9
votes
6answers
11k views

1000 digits of pi in python

I have been thinking about this issue and I can't figure it out. Perhaps you can assist me. The problem is my code isn't working to output 1000 digits of pi in the python coding language. Here's my ...
10
votes
2answers
10k views

How does this class implement the “__iter__” method without implementing “next”?

I have the following code in django.template: class Template(object): def __init__(self, template_string, origin=None, name='<Unknown Template>'): try: template_string = ...
17
votes
5answers
818 views

Algorithm (prob. solving) achieving fastest runtime

For an algorithm competition training (not homework) we were given this question from a past year. Posted it to this site because the other site required a login. This is the problem: ...
8
votes
6answers
4k views

Behaviour of Python's “yield”

I'm reading about the yield keyword in python, and trying to understand running this sample: def countfrom(n): while True: print "before yield" yield n n += 1 ...
7
votes
2answers
11k views

Python EOF for multi byte requests of file.read()

The Python docs on file.read() state that An empty string is returned when EOF is encountered immediately. The documentation further states: Note that this method may call the underlying C ...
8
votes
2answers
6k views

What's the best way to split a string into fixed length chunks and work with them in Python?

I am reading in a line from a text file using: file = urllib2.urlopen("http://192.168.100.17/test.txt").read().splitlines() and outputting it to an LCD display, which is 16 characters wide, in a ...
5
votes
5answers
2k views

Recursive generator for flattening nested lists

I'm a programming newbie and am having some trouble understanding an example from my python textbook ("Beginning Python" by Magnus Lie Hetland). The example is for a recursive generator designed to ...
13
votes
3answers
2k views

Javascript Generators: Understanding them

I'm pretty sure my understanding of generators is inherently broken. All online resources seem to conflict and it makes for an incredibly difficult and confusing learning experience. From what I ...
2
votes
7answers
355 views

Pythonic way to implement three similar integer range operators?

I am working on a circular problem. In this problem, we have objects that are put on a ring of size MAX, and are assigned IDs from (0 to MAX-1). I have three simple functions to test for range ...
0
votes
3answers
7k views

Returns values from a for loop in python

I'm trying to figure out the syntax for passing arguments from one list or dict to another in the for loop syntax. The desired result I'm looking for is this: for bean in beans: if bean.type == ...
5
votes
5answers
2k views

What are the advantages of “yield item” vs return iter(items)?

In the examples below, resp.results is an iterator. Version1 : items = [] for result in resp.results: item = process(result) items.append(item) return iter(items) Version 2: for result ...
6
votes
4answers
1k views

Don't understand this python For loop

I'm still a python newb, but I'm working through the Pyneurgen neural network tutorial, and I don't fully understand how the for loop used to create the input data works in this instance: for ...
9
votes
3answers
2k views

Subclassing and overriding a generator function in python

I need to override a method of a parent class, which is a generator, and am wondering the correct way to do this. Is there anything wrong with the following, or a more efficient way? class A: def ...

15 30 50 per page