Search code examples
pythontwitterself

Calling a python function that requires self


I'm trying to call a method in the class "User" in the twitter python api (Line 653): https://code.google.com/p/python-twitter/source/browse/twitter.py. All of the methods require a 'self' argument, how do I pass it to the method? I read how to use 'self' but didn't figure it out.

I have used:

user = twitter.User()

name = user.GetName() #self needs to be passed here

print str(name)

This outputs "None". So my question is how to properly get the self to send it as an argument.

I'm new to python and to the twitter api so any help is much appreciated.


Solution

  • self is passed; since user is an instance of the User class, the following calls are identical:

    name = user.GetName()
    name = User.GetName(user)
    

    self is the implicit first argument, set to the value of the object that invokes the method, in any instance method.

    Since you are getting None as output, this means GetName is returning None. If you had a problem with passing the argument, you would be getting an actual error in the call to GetName. Perhaps you need to specify a name when you call User so that GetName has something to return?