Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.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_Inheritance_Singleton - Fatal编程技术网

更改实例返回python的内容

更改实例返回python的内容,python,inheritance,singleton,Python,Inheritance,Singleton,我已经为SeleniumWebDriver创建了一个单例包装器 class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return

我已经为SeleniumWebDriver创建了一个单例包装器

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class Driver(metaclass=Singleton):   
        """Ensures theres only 1 Driver open at any time.
        If the driver is closed, tries to quit the previous
        instance, and recreates a new one"""

        def __init__(self):
            self._driver = self.load_selenium_driver()

    @staticmethod
    def load_selenium_driver():
        _chrome_driver_locn = CHROME_DRIVER_LOCATION
        _driver =  webdriver.Chrome(_chrome_driver_locn)
        return _driver

    @property
    def driver(self):
        """Creates a new driver if the previous one is closed,
        and quits the instance of the chrome driver."""
        if self._web_browser_is_open():
            return self._driver
        else:
            try:
                self._driver.quit()
            finally:
                self._driver = self.load_selenium_driver()
                return self._driver

    def _web_browser_is_open(self):
        try:
            self._driver.title
            return True
        except WebDriverException:
            return False
正在运行
a=Driver()
我想访问
a
中的
Driver.Driver
方法,这样我就不需要执行
a.Driver.get('google.com')
,而是
a.get('google.com')

问题是:


如何创建驱动程序实例,返回
Driver
对象(可在属性
Driver.Driver
中找到),而不是
Driver
的实例?

根据评论中的讨论,我所要求的是不可能的

Foo()
不“调用”类实例,而是创建一个类实例。你为什么要这样做?问题的背景是什么?为什么不直接使用
a=5
?使用标识符表达式将始终返回标识符绑定到的对象。它不会返回其他内容。你是什么意思?@啊,你在评论中要求的和你的问题要求的是两个完全不同的东西。我重新打开了问题,删除了开始代码(并用更相关的代码替换)