为什么不能以这种方式使用class attr?

时间:2019-05-29 07:28:27

标签: python python-3.x

为什么不能以这种方式使用attr类?

SELECT x.date
     , x.user_id
     , y.event_name
  FROM clickstream_video x
  LEFT
  JOIN clickstream_video y
    ON y.date = x.date + INTERVAL 1 DAY
   AND y.user_id = x.user_id
WHERE x.event_name = 'video_play';

,错误消息如下:

await createIndex("cases", { "info.relatedEntities.role": 1 } ),
await createIndex("entities", { "info.name": 1 } ),
const caseCollection = await getCaseCollection();
const cases = caseCollection.aggregate([
    { $match: { "info.relatedEntities.role": role} },
    { $lookup: {
            from: "entities",
            localField: "info.relatedEntities.entity",
            foreignField: "_id",
            as: "relatedEntities"
        }},
    { $match: { "relatedEntities.info.name": { $regex: name}}},
    { $limit: limit }
]);

如果我将它写在类的外面,不会有错误,类似于以下代码:

import { LocationStrategy } from '@angular/common';  
 constructor( private location: LocationStrategy){  
// preventing back button in browser implemented by "Samba Siva"  
history.pushState(null, null, window.location.href);
this.location.onPopState(() => {  
history.pushState(null, null, window.location.href);
});  
}

1 个答案:

答案 0 :(得分:0)

关于类,实例变量在init方法内定义,如下所示:

class A:
    def __init__(self):
        self.a = 1
        self.d = [1, 2, 3, 4]
        self.e = [i for i in self.d if i == self.a]
        print(self.e)

# Creating an object of the class in order to trigger __init__
A()

如果直接在类内部定义变量,则它们将是类变量。可以通过类实例(通常是实例变量)来访问它们,也可以直接从类名(在您的情况下为A.a)来访问它们。

根据我的测试,您的代码可能会失败,因为列表理解的范围不包含a。我不确定这是否是因为类A仍在构造中,因此缺少适当的传递范围。我之所以这样说,是因为用for循环代替理解会产生预期的结果:

class A:
    a = 1
    d = [1, 2, 3, 4]
    e = []
    for i in d:
        if i == a:
            e.append(i)
    print(e)
    # [1]