【问题标题】:What does deconstructible do in Python?可解构在 Python 中做了什么?
【发布时间】:2017-05-04 10:36:56
【问题描述】:

Python 中的deconstructible 装饰器有什么作用?我遇到了这个装饰器在看别人的代码,但是我搜索了 Python 文档并找不到这个装饰器实际上做了什么?

@deconstructible
class UserValidator(object):
    def __call__(self, value):
        if value:
           etc

鉴于上面的代码,在这个类中添加“可解构”有什么作用?

【问题讨论】:

标签: python django python-3.x


【解决方案1】:

通过使用 Google,我设法发现它是 Django 的一部分:

https://docs.djangoproject.com/en/stable/topics/migrations/#adding-a-deconstruct-method

只要类的构造函数的所有参数本身都是可序列化的,就可以使用 django.utils.deconstruct 中的 @deconstructible 类装饰器来添加 deconstruct() 方法

【讨论】:

    【解决方案2】:
    def deconstructible(*args, **kwargs):
        """
        Class decorator that allow the decorated class to be serialized
        by the migrations subsystem.
    
        Accepts an optional kwarg `path` to specify the import path.
        """
    

    【讨论】:

      猜你喜欢
      • 2018-02-21
      • 2019-01-13
      • 2020-10-13
      • 2018-12-04
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2013-07-09
      相关资源
      最近更新 更多