Luke Webb
Luke Webb

Reputation: 23

Python - Class Scope

I have a python class that works great.

However I need to replace a function from another class as part of my class.

class myclass:

   def display(self, jobid):   #Self here is referencing my class
       zoom = NavigationToolbar2.zoom
       def new_zoom(self, *args, **kwargs):
           if self.blurEnabled:    #Self is referencing NavigationToolbar2 Instance instead of my class
               self.toggleBlur()   #Self is referencing NavigationToolbar2 Instance instead of my class
           zoom(self, *args, **kwargs)
       NavigationToolbar2.zoom = new_zoom

The code here works great, except for I am unable to acces my own class so my own functions fail. Is anyone able to help??

Upvotes: 1

Views: 145

Answers (1)

Krumelur
Krumelur

Reputation: 32497

The reason is that the variable self gets overloaded in your inner scope. Try renaming it, e.g.

class myclass:

   def display(self, jobid):   #Self here is referencing my class
       zoom = NavigationToolbar2.zoom
       def new_zoom(toolbar, *args, **kwargs):
          ...
       NavigationToolbar2.zoom = new_zoom

Upvotes: 1

Related Questions