Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在模块之间构造重用代码_Python_Module - Fatal编程技术网

Python 如何在模块之间构造重用代码

Python 如何在模块之间构造重用代码,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

我对Python非常陌生,在编写应用程序时,最终的结构有点混乱。下面的例子应该说明我正在尝试做什么。问题是我不能从common.py调用login方法,因为它只在website1.py或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
模块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登录

其次,可以将类引用传递给
浏览器
类。(如果您接受我的第一个建议,请提供模块参考)