【问题标题】:TypeError: __init__() got multiple values for keyword argument 'customer'TypeError:__init__() 为关键字参数“客户”获取了多个值
【发布时间】:2017-09-28 03:55:25
【问题描述】:
customer = CustomerProfile.objects.get(pk=4)
ipdb> SimilarCustomerFinder(self, customer=customer, fields=self.fields)
*** TypeError: __init__() got multiple values for keyword argument 'customer'

SimilarCustomerFinder 课上,我有

def __init__(self, customer, fields):
    self._matches = {}
    props = self.__class__.__dict__.keys()
    self.customer = customer
    self.fields = fields
    self.checks = [k for k in props if k.startswith('check_')]
    if customer:
        self.user_id = customer.user.pk
    else:
        self.user_id = -1

    for check in self.checks:
        c = check.replace('+', '_')
        getattr(self, c)()

我正在努力解决这个错误。我该如何解决?如果我删除customer=customer,我得到*** AttributeError: 'CustomerUpdateForm' object has no attribute 'user',为什么?

【问题讨论】:

  • 这段代码没有意义。第一个 sn-p 中的 self 是什么?

标签: python


【解决方案1】:

鉴于ipdb 输出,您似乎正在尝试使用此命令创建一个实例:

SimilarCustomerFinder(self, customer=customer, fields=self.fields)

但是self 是一个隐式传递的参数,所以你不应该显式地传递它。像这样:

SimilarCustomerFinder(customer=customer, fields=self.fields)

或者,如果您真的打算显式传递它(这真的很奇怪,并且可能不会按照您的意图进行 - 但谁知道......)您必须在类上显式调用该方法:

SimilarCustomerFinder.__init__(self, customer=customer, fields=self.fields)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多