gpt4 book ai didi

python - Python 中的元类基是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:20 24 4
gpt4 key购买 nike

我正在尝试扩展 django-allauth 来做一些特定于我的项目的事情。我基本上是在尝试在 django-allauth 之上编写自己的包装器,并希望安装、配置和其他内容与 allauth 非常相似。

为此,我首先从 allauth/accounts/app_settings.py 扩展 AppSettings 类。我创建了自己的 app_settings.py 做了这样的事情:

from allauth.account import app_settings as AllAuthAppSettings
class MyAppSettings (AllAuthAppSettings):
def __init__(self, prefix):
# do something

此外,在 app_settings.py 的末尾,我简单地添加了以下内容(从 django-allauth 本身复制):

import sys
my_app_settings = MyAppSettings('MY_PREFIX_')
my_app_settings.__name__ = __name__
sys.modules[__name__] = my_app_settings

现在,当我启动项目时,出现以下错误:

TypeError: Error when calling the metaclass bases
__init__() takes exactly 2 arguments (4 given)

老实说,我对 Python-Django 世界还很陌生,并且不太明白最后四行发生了什么。

什么是元类基?传递给它的四个参数是什么?我如何使这个流程发挥作用?

这是堆栈跟踪:

Unhandled exception in thread started by <function wrapper at 0x104146578>
Traceback (most recent call last):
File "/Users/user/anaconda/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/user/anaconda/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Users/user/anaconda/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Users/user/anaconda/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/user/anaconda/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/user/anaconda/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/user/anaconda/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/Users/user/anaconda/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/user/myproject/my_app/models.py", line 18, in <module>
from .model_managers import *
File "/Users/user/myproject/my_app/model_managers.py", line 89, in <module>
from . import app_settings
File "/Users/user/myproject/my_app/app_settings.py", line 9, in <module>
TypeError: Error when calling the metaclass bases
__init__() takes exactly 2 arguments (4 given)

最佳答案

看起来您不应该能够继承AllAuthAppSettings

django-allauth 包正在执行一些非常丑陋的 Python 魔法

import sys  # noqa
app_settings = AppSettings('ACCOUNT_')
app_settings.__name__ = __name__
sys.modules[__name__] = app_settings

基本上,当您导入 app_settings 模块时,它会创建 AppSettings 类的实例,并将其重命名为 app_settings 的名称模块,然后用类的实例替换导入的模块!

您不能从类实例继承。我猜您想从非实例化的 AppSettings 类继承。为此,您需要继承 app_settingsclass,而不是直接继承 app_settings

from allauth.account import app_settings as AllAuthAppSettings


class MyAppSettings(AllAuthAppSettings.__class__):
...

我认为您不需要从 app_settings 模块末尾复制这些行来将您的模块破解为类。

关于python - Python 中的元类基是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39879345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com