alan
alan

Reputation: 4457

How to structure re-used code between modules

I'm pretty new to Python and, in writing an app, have ended up with a structure that's a bit of a mess. The example below should illustrate what I'm trying to do. The issue is that I can't call the login method from common.py because it is only defined in website1.py or website2.py.

Module common.py

class Browser():

    def load_page():
        Login.login()

Module website1.py import common.py

class Login:
    @staticmethod        
    def login():
        #code to login to this website 1

Module website2.py import common.py

@staticmethod
class Login:
    def login():
        #code to login to website 2

Any thoughts on how to restructure this would be appreciated.

Upvotes: 0

Views: 119

Answers (1)

Pwnna
Pwnna

Reputation: 9538

First of all, why static methods? You could just do def login at the global level.

Second of all, you could pass a class reference to the Browser class. (or a module reference if you take my first suggestion)

class Browser(object):
    def __init__(self, loginCls):
        self.loginCls = loginCls

    def login_page(self):
        self.loginCls.login()

Upvotes: 1

Related Questions