Question

I am trying to use Twython to work with the Twitter API in python, and I am observing some behavior that seems odd to me.

If I run the following code...

from twython import Twython
from random import randint

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) # In the actual code, I obviously assign these, but I can't disclose them here, so the code won't work...

user_id = randint(1,250000000)
twitter_user = twitter.lookup_user(user_id)

I get this error.

Traceback (most recent call last):

File "Twitter_API_Extraction.py", line 76, in <module>
twitter_user = twitter.lookup_user(user_id) # returns a list of dictionaries with all the users requested
TypeError: lookup_user() takes exactly 1 argument (2 given)

The Twython docs indicate I only need to pass the user id or the screen name (https://twython.readthedocs.org/en/latest/api.html). Some googling suggested that this error usually mean I need to pass self as the first parameter, but I didn't quite get why.

However, if I use the following assignment instead...

twitter_user = twitter.lookup_user(user_id = randint(1,250000000))

Everything comes up roses. I can't figure out why this is, and it is a problem later in the code when I am trying to access followers using the same lookup_user function.

Any clarification on what is triggering this error and how I am bypassing it via assignment in the function call would be most appreciated!

Was it helpful?

Solution

Per the API documentation:

lookup_user(**params)

Returns fully-hydrated user objects for up to 100 users per request, as specified by comma-separated values passed to the user_id and/or screen_name parameters.

The ** syntax (documentation) means you need to provide named arguments (i.e. f(a=b)), in this case user_id and/or screen_name

In your first attempt you're trying to pass a positional argument (i.e. f(a)), which the function is not set up for.

OTHER TIPS

The API states that lookup_user takes keyword arguments only. Keyword arguments take the form keyword=value, which is what you are doing with lookup_user(user_id=randint(1,...)). It means you cannot pass positional arguments, which is what you are doing with lookup_user(userid).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top