0

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.

Adam
  • 2,948
  • 10
  • 43
  • 74
  • There's no way you'll be able to access local variables from the base class method in your overriding method. As for the `TypeError`, you'll need to show the code you're using to get that error. All you show here is the class definition, but how are you using this class? – BrenBarn Sep 17 '13 at 19:09
  • @BrenBarn What is the next best thing I could do then? Is the only solution in this case copying the whole method from the parent class? – Adam Sep 17 '13 at 19:11
  • What exactly are you trying to accomplish by overriding the method? – BrenBarn Sep 17 '13 at 19:11
  • Yes, if `result` and `next_cursor` are local variables, and aren't stored or returned or exposed in any other way, there's no way to access them, short of copying the source of the function that defines them and running that instead of the original. – abarnert Sep 17 '13 at 19:12
  • 2
    Looking at the code you linked `next_cursor` isn't even a local variable in that function, it's an element in a dict or something that gets reloaded repeatedly. It looks like you will have to copy that method and modify it if you want to customize that process. – BrenBarn Sep 17 '13 at 19:14
  • At least `result` isn't a problem; the function always calls `return result`, so you just need to do `result = super(MyApi, self).GetFollowerIDs(*args, **kwargs)` to get that… – abarnert Sep 17 '13 at 23:39
  • @abarnert But to get to data['next_cursor'] there's absolutely no way but to copy the whole function. Correct? – Adam Sep 18 '13 at 01:57
  • 1
    @AdamSilver: Right. In fact, as BrenBarn explained, `data` is reassigned each time through a loop, so it doesn't even make sense to get `data['next_cursor']`; you have to get each of the values that `data['next_cursor']` takes over time, which means your code has to be inside the loop (or you have to stash the values in a list or something), which means you have to copy and edit the function. – abarnert Sep 18 '13 at 17:15

2 Answers2

2

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.

abarnert
  • 354,177
  • 51
  • 601
  • 671
-2

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
Nacib Neme
  • 859
  • 1
  • 17
  • 28