Python 如何在模块之间构造重用代码
我对Python非常陌生,在编写应用程序时,最终的结构有点混乱。下面的例子应该说明我正在尝试做什么。问题是我不能从common.py调用login方法,因为它只在website1.py或website2.py中定义 模块common.pyPython 如何在模块之间构造重用代码,python,module,Python,Module,我对Python非常陌生,在编写应用程序时,最终的结构有点混乱。下面的例子应该说明我正在尝试做什么。问题是我不能从common.py调用login方法,因为它只在website1.py或website2.py中定义 模块common.py class Browser(): def load_page(): Login.login() class Login: @staticmethod def login(): #cod
class Browser():
def load_page():
Login.login()
class Login:
@staticmethod
def login():
#code to login to this website 1
@staticmethod
class Login:
def login():
#code to login to website 2
模块website1.py
导入common.py
class Browser():
def load_page():
Login.login()
class Login:
@staticmethod
def login():
#code to login to this website 1
@staticmethod
class Login:
def login():
#code to login to website 2
模块website2.py
导入common.py
class Browser():
def load_page():
Login.login()
class Login:
@staticmethod
def login():
#code to login to this website 1
@staticmethod
class Login:
def login():
#code to login to website 2
任何关于如何重组的想法都将不胜感激。首先,为什么是静态方法?您可以在全局级别执行
def登录
其次,可以将类引用传递给浏览器
类。(如果您接受我的第一个建议,请提供模块参考)