applying Singleton to Spotipy throws error

huangapple go评论78阅读模式
英文:

applying Singleton to Spotipy throws error

问题

Spotipy的对象需要传递一个凭据的kw参数
我的代码:

import spotipy
class Spoti2(spotipy.Spotify):
    spoty_obj = None
    
    @classmethod
    def __new__(cls, client_credentials_manager):
        if cls.spoty_obj is None:
            cls.spoty_obj = super().__new__(cls)
            print('实例已创建')
        return cls.spoty_obj

但是我得到了错误:

Spoti2.__new__()为参数'client_credentials_manager'获得了多个值

然而,当我移除@classmethod时,它可以工作,我不知道为什么

英文:

Spotipy's object needs to be passed a credential kw arg
my code:

import spotipy
class Spoti2(spotipy.Spotify):
    spoty_obj = None
    
    @classmethod
    def __new__(cls, client_credentials_manager):
        if cls.spoty_obj is None:
            cls.spoty_obj = super().__new__(cls)
            print('se creo instancia')
        return cls.spoty_obj

but i get the error:

Spoti2.__new__() got multiple values for argument 'client_credentials_manager'

however, when I remove @classmethod, it works, and I don't know why

答案1

得分: 1

"你不应该将 @classmethod 添加到 __new__()"

来自:https://docs.python.org/3/reference/datamodel.html#object.new

特殊情况,因此您无需将其声明为[静态/类方法]。

英文:

You're not supposed to add @classmethod to __new__()

From: https://docs.python.org/3/reference/datamodel.html#object.__new__

> special-cased so you need not declare it as [a static/class method]

huangapple
  • 本文由 发表于 2023年5月11日 05:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定