Python数组语法

时间:2015-09-17 12:23:26

标签: php python arrays

可能是一个愚蠢/简单的问题,但我应该如何在python中插入这种类型的数组符号。

self.sessions[(recepientId, deviceId)]

我正在尝试将一些Python翻译成PHP。

正如有人说不清楚,对我来说更不清楚,所以我会把全部功能。但我不能做得更多,我不太了解蟒蛇。

   def loadSession(self, recepientId, deviceId):
    if self.containsSession(recepientId, deviceId):
        return SessionRecord(serialized=self.sessions[(recepientId, deviceId)])
    else:
        return SessionRecord()

sessions变量的定义是这样的。

self.sessions = {}

2 个答案:

答案 0 :(得分:2)

(recepientId, deviceId)是一个由两个对象组成的元组。 self.sessions是字典(即键值哈希映射)。

元组是,通过执行self.sessions[(recepientId, deviceId)]从字典中检索关联值。 因此,它只是查找哈希映射中的特定值(在Python中称为字典)。

答案 1 :(得分:1)

这段代码只是试图通过key在self.sessions(它的键值映射)中找到SessionRecord,其中key是一对recipientId和deviceId。

在C ++,Java,C#等中,人们宁愿写下这样的东西:

class Key {
//constructor
Id recipientId;
Id deviceId;
//getters|setters and stuff
}
Map<Key, SessionRecord> sessions = Store.getSessions();
SessionRecord session = sessions.get(new Key(recipientId, deviceId));