导入还是不导入classmethod?

法比奥

我希望这不是一个愚蠢的问题,但是我在导入的地方找到了一些代码,在没有导入的地方找到了一些代码,classmethod所以有区别吗?

我正在使用python 3.6,但我最初认为的代码适用于python 2.7(它使用了from __builtin__ import

import unittest
from selenium import webdriver
from builtins import classmethod #original code was from __builtin__ import classmethod 


class HomePageTest(unittest.TestCase):
    @classmethod
    def setUp(cls):
        # create a new Firefox session
        cls.driver = webdriver.Firefox()
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()

        # navigate to the application home page
        cls.driver.get("http://demo-store.seleniumacademy.com/")

    def test_search_field(self):
        pass

    #My tests without @classmethod

    @classmethod
    def tearDown(cls):
        # close the browser window
        cls.driver.quit()

if __name__ == '__main__':
    unittest.main(verbosity=2)
塞弗特

通常,您仅导入,builtins或者__builtin__如果您的代码中也有一个变量与内置名称相同,并且还想访问内置名称,则该操作才可用。该模块的文档对此进行了很好的解释:

Builtins —内置对象

该模块可直接访问Python的所有“内置”标识符;例如,builtins.open是内置函数的全名open()有关文档,请参见内置函数内置常量

通常,大多数应用程序都不会显式访问此模块,但是在为对象提供与内置值同名的对象的模块中很有用,但其中还需要该名称的内置物。例如,在要实现open()包装内置函数的open()模块中,可以直接使用此模块:

import builtins

def open(path):
    f = builtins.open(path, 'r')
    return UpperCaser(f)

class UpperCaser:
    '''Wrapper around a file that converts output to upper-case.'''

    def __init__(self, f):
        self._f = f

    def read(self, count=-1):
        return self._f.read(count).upper()

但是,根据您的情况,文件中似乎没有classmethod定义,因此您实际上不需要from builtins import classmethod

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章