Snowlav Snowlav - 1 year ago 59
Python Question

Python and nesting classes for structure

I am having a bit of trouble with nesting classes in python.

Mind you my code below is a simplified example showing what I want to do, but basically I want to use nested classes to make my code more structured and make sure I don't run into name clashes for certain functions.

See here my example code:

class Chrome:

def __init__(self, url='http://localhost:4723/wd/hub'):

# Capabilities
capabilities =

# Start session
self.driver = webdriver.Remote(url, capabilities)

def get_url(self, url):

# Go to URL

class actions:

def browse_url(url):

# Go to url

if __name__ == '__main__':

browser = Chrome()


The goal as you can see in
if __name__ == '__main__'
is to be able to start a browser instance, and then call functions in a structured way.

However I have no clue on how to correctly achieve the

How is this done correctly ?

Answer Source

You should call get_url from an instance of Chrome and not the class itself, since it's an instance method and not a static one:

def browse_url(url):

if __name__ == '__main__':
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download