Python 被 @staticmethod 装饰的方法在调用时,会调用 __init__ 方法?

2020-11-10 21:43:53 +08:00
 AbcHiyi

被测试代码入如下:

class Config:
    def __init__(self, save_path=False, file_name="config.ini"):
        if save_path:
            try:
                self.tgt_lang = self.read_inf(file_name, save_path)
            except errors.FileError:
                self.tgt_lang = update_language_code()
                self.save_ini(file_name, save_path, self.tgt_lang)
        else:
            self.tgt_lang = update_language_code()

    @ staticmethod
    @ file_check
    def read_inf(file_name, path):
        """读取配置文件"""
        c_p = ConfigParser()
        c_p.read(os.path.join(file_name, path), encoding='UTF-8')

        return {i: dict(c_p.items(i)) for i in c_p.sections()}

    @ staticmethod
    def save_ini(file_name, path, data):
    	"""保存配置文件"""
        c_p = ConfigParser()
        c_p.read(path, encoding='UTF-8')

        for section in data.keys():
            for option in data_table[section].keys():
                try:
                    c_p.set(section, option, data_table[section][option])
                except NoSectionError:
                    c_p.add_section(section)
                    c_p.set(section, option, data_table[section][option])

        path = Path(path)
        if not path.is_dir():
            path.mkdir()

        full_path = os.path.join(path, file_name)
        with open(full_path, 'w', encoding='UTF-8') as file:
            c_p.write(file)

这边是测试代码:

class Config(unittest.TestCase):

    def setUp(self):
        self.save_dir = os.path.join(BASE_DIR, 'test_files')

    def tearDown(self):
        # 清扫文件
        try:
            shutil.rmtree(self.save_dir)
        except FileNotFoundError:
            pass


    def test_read_config(self):
     *1 setting.Config(self.save_dir)
     *2 setting.Config.save_ini(
            'config.ini', self.save_dir,
            {
                "test": {'test-check': 'True'}
            }
        )

        config = setting.Config(self.save_dir)

        if 'test' not in config.tgt_lang:
            self.fail('未能从本地文件读取数据')

主要问题是,在测试时,我在*1 和*2 的位置打了断点,并在被测类的'_init_' 方法内打了断点。 发现在调用*2 处的静态方法时,vscode 会跳转到被测类的'_init_'方法中. 我就纳闷了,怎么调用静态方法还会调用'_init_'方法,这不应该啊.

update_language_code()这个函数下载数据挺耗时间的,都是尽量能保存到本地就使用配置文件中的数据,不行再从服务器中下载

下面的代码是一个简化之后的样子,

class B:
    def __init__(selsf):
        print('b class')


class A:
    def __init__(self):
        print('a class')
        self.b = B()

    @staticmethod
    def b():
        print("A.b method")

在单独初始化'A 时'

A()

控制台打印

a class
b class

在调用静态方法 A.b 时

A.b()

控制台打印

b method

这让我更迷惑了,这和我设想的时一样的。但是 vscode 调试代码时为什么还是会跳转到'_init_'代码块?? 让人感觉就像是其中的代码被执行了一样

2580 次点击
所在节点    Python
23 条回复
AbcHiyi
2020-11-11 08:32:37 +08:00
结案了,粗心大意,造成的哈哈哈
freakxx
2020-11-11 14:03:18 +08:00
@AbcHiyi #20

哈哈哈,不过我看你两次代码,感觉你这样写 python 爆炸概率很高,

比如像你在 init 做的操作
拆出来比较好,

init 直接定义路径,不做一些操作。
再定义一个子函数,在用的时候,再进行判断,没有的话再 update 。

-----------------

不过我很喜欢你的贴,总有一些弯弯绕绕让我尝试领会另一种奇特姿势。
AbcHiyi
2020-11-23 16:16:22 +08:00
@freakxx 哈哈喜欢尝试一些新姿势,挺有趣的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/723746

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX