Skip to content Skip to sidebar Skip to footer

Getting Nested Variables In __init__function

Code: class GmailFarming(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) def StartSeleniumScript(): with open('json/Gmail_farmi

Solution 1:

Reading your code, I assume what you are trying to do is creating a class with an __init__ method that defines an auxiliary function StartSeleniumScript in which you declare two variables email_for_account_to_run and password_for_account_to_run from which you want to recover the values outside of the class.

At the least I find your code confusing because:

  • you mix object-oriented style programming with functional style (instead you could have created an auxiliary method startSeleniumScript(self) at the same level as the __init__ method rather than nesting a function definition inside it)
  • the nested function you create is never even called
  • the nested function you create has no return value
  • the class has no instance variable declared in the __init__ method
  • you never create an instance of the class

All this leaves me a bit perplex as to what exactly you want to do but something like this would work:

classGmailFarming:
    
    def__init__(self, parent, controller):
        
        defStartSeleniumScript():
            self.foo = 1
            self.bar = 2
        
        StartSeleniumScript()
        
g = GmailFarming(None,None)
print(g.foo)
print(g.bar)

In essence, you add a self. in front of the variables that you would like to access so that they become instance variables that you can access from the outside. And of course you have to create an object of the class first before you can get the instance variables out of it.

Personally, I would have defined the nested function as an auxiliary method like this, however:

classGmailFarming:
    
    def__init__(self, parent, controller):
        self.__startSeleniumScript()
        
    def__startSeleniumScript(self):
        self.foo = 1
        self.bar = 2
    
g = GmailFarming(None,None)
print(g.foo)
print(g.bar)

Post a Comment for "Getting Nested Variables In __init__function"