我使用下面的代码创建了Singleton类(将self作为参数传递给 new )
>>> class Singleton(object):
... IsSingleton = None
... def __new__(self):
... if self.IsSingleton == None:
... print "Singleton IF"
... self.IsSingleton = super(Singleton, self).__new__(self)
... print "ELSE"
... return self.IsSingleton
并使用cls作为 new
的参数创建了相同的内容 >>> class Singleton(object):
... IsSingleton = None
... def __new__(cls):
... if cls.IsSingleton == None:
... print "Singleton IF"
... cls.IsSingleton = super(Singleton, cls).__new__(cls)
... print "ELSE"
... return cls.IsSingleton
我的问题是,如果 new 是静态方法,那么如何将self传递给静态方法不会出错?将self或cls传递给 new 之间有什么区别?只要我读过,cls就会传递给 new 而不是self。两个不同的代码集中的行为是否相同???
答案 0 :(得分:0)
调用参数SELECT a.id , a.name , SUM(ds.downloads) AS 'downloads'
FROM apps a LEFT JOIN downloads_stats ds ON a.id = ds.app_id
GROUP BY a.id , a.name
ORDER BY downloads DESC
LIMIT 10;
或cls
只是惯例。您可以将其称为self
或任何您想要的内容。这不会改变这种行为。但它是一个类,所以x
是它的适当名称。