문제

I am trying to override the method called GetFollowerIDs of this class: https://github.com/bear/python-twitter/blob/master/twitter.py#L3705

What I want to achieve is executing the function normally then get next_cursor not just result.

I tried the following:

class MyApi(twitter.Api):
    def GetFollowerIDs(self, *args, **kwargs):
        super(MyApi, self).GetFollowerIDs(*args, **kwargs)

        print result
        print next_cursor

I got this error:

TypeError: unbound method GetFollowerIDs() must be called with MyApi instance as first argument (got nothing instead)

When calling it like this:

ids = MyApi.GetFollowerIDs(
                    screen_name=options['username'],
                    cursor=cursor,
                    count=options['batch-size'],
                    total_count=options['total'],
                )

On top of that, result and next_cursor are already showing as not defined in my IDE.

도움이 되었습니까?

해결책

The TypeError has nothing to do with your definition, but with your call:

ids = MyApi.GetFollowerIDs(
                    screen_name=options['username'],
                    cursor=cursor,
                    count=options['batch-size'],
                    total_count=options['total'],
                )

GetFollowerIDs is an instance method—that's why it takes a self parameter. So you have to call it on an instance of the class, not the class itself.

The API documentation samples show how to properly create and use an instance of twitter.API; you'll do the exact same thing, except to create and use an instance of MyApi instead.

You also may want to read the tutorial on Classes, or some third-party tutorial, if this isn't obvious once pointed out.


Meanwhile, within the method, you're calling the base class properly via super… but that isn't going to let you access local variables from the base class method. Local variables are local; they only live while the method is running. So, after the base class method returns, they don't even exist anymore.

The reason your IDE is saying they aren't defined is that they really aren't defined, except within the implementation of that method.

If you really need to access the internal state of the method's implementation, the only reasonable workaround would be to copy that method's implementation to your code, instead of calling the method.

다른 팁

The problem is that your forget the argument self in line 3 when calling GetFollowerIDs:

class MyApi(twitter.Api):
    def GetFollowerIDs(self, *args, **kwargs):
        super(MyApi, self).GetFollowerIDs(self,*args, **kwargs)

        print result
        print next_cursor
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top