Take the 2-minute tour ×
# -*- coding: utf-8 -*-
import os
import fbconsole
here = os.path.dirname(os.path.abspath(__file__))

def fbfeed():
    fbconsole.APP_ID = '588914247790498'
    fbconsole.AUTH_SCOPE = ['publish_stream', 'publish_checkins', 'read_stream', 'offline_access']
    newsfeed = fbconsole.get('/me/home')
    newsfeedData = newsfeed["data"]
    for status in newsfeedData:
        fromn = [status['from']['name']]
        name = [status.get('name', None)]
        description = [status.get('description', None)]
        if description == name is None:
            return fromn
        elif description is None:
            return fromn.extend(name)
        elif name is None:
            return fromn.extend(description)
            return fromn + name + description

My code returns only one string, but when I use print instead of return - it prints ALL of results. How can I return the same results, as in case of print??

share|improve this question
change you returns for yield then you can for f in feeds(): –  sotapme Feb 11 '13 at 18:40
add comment

2 Answers

up vote 2 down vote accepted

When you use return it exits the function and doesn't keep iterating through the loop like it does with print. Try yield.

share|improve this answer
ok, it works, and how can I define this result to a variable? I want to send it to a mako template. –  yakudza_m Feb 11 '13 at 19:07
When you use the yield statement it turn the function into a generator, which can be used a lot like a list. The easiest way to convert this to a list is just to say x = list(fbfeed()) –  Hoopdady Feb 11 '13 at 19:31
it shows me output like a [u'\u0406\u0440\u0438\u043d\u0430 \u0421\u0435\u0445', None, u'Change Ur Template to R3D Here--> http://bit.ly/14NYfaJ?477259&cid=theme'] how to fix it??? –  yakudza_m Feb 11 '13 at 22:51
That's probably worthy of starting another question. I believe those are unicode characters. Python has a str.decode('utf-8') or something like that, but I'm not too familiar with it. –  Hoopdady Feb 12 '13 at 14:15
add comment

The issue is that when your loop hits the first return statement the function will exit and the loop will not continue. Using print will allow the loop to continue.

Two options are to create a list before you start looping, add the statuses to the list in the loop, and then return the list after the loop.

Using the yield keyword instead of return will allow other functions to loop over the result. More details on the yield keyword can be found here: The Python yield keyword explained (plus in the actual docs: http://docs.python.org/2.7/reference/expressions.html?highlight=yield#yield-expressions).

share|improve this answer
add comment

Your Answer


