Python定义的代码

时间:2014-01-02 17:33:45

标签: python

FUNCTIONS = {
    "I2C": {"enabled": False, "gpio": {0:"SDA", 1:"SCL", 2:"SDA", 3:"SCL"}, "modules": ["i2c-bcm2708", "i2c-dev"]},
    "SPI": {"enabled": False, "gpio": {7:"CE1", 8:"CE0", 9:"MISO", 10:"MOSI", 11:"SCLK"}, "modules": ["spi-bcm2708", "spidev"]},
    "UART": {"enabled": False, "gpio": {14:"TX", 15:"RX"}},
    "ONEWIRE": {"enabled": False, "gpio": {4:"DATA"}, "modules": ["w1-gpio"], "wait": 2}
}

不确定这个,是一个数组吗?字典? {}和[]?键和值对?

1 个答案:

答案 0 :(得分:3)

FUNCTIONS是一本字典,直接值也是字典。

嵌套词典中的gpio键是更多词典,modules键与列表对象相关联。

Python容器可以容纳其他容器;带有{...}对的key: value花括号形成一个字典,[...]方括号定义列表。

您可以在Python解释器中内省并使用该结构; type()函数会告诉您对象类型:

>>> FUNCTIONS = {
...     "I2C": {"enabled": False, "gpio": {0:"SDA", 1:"SCL", 2:"SDA", 3:"SCL"}, "modules": ["i2c-bcm2708", "i2c-dev"]},
...     "SPI": {"enabled": False, "gpio": {7:"CE1", 8:"CE0", 9:"MISO", 10:"MOSI", 11:"SCLK"}, "modules": ["spi-bcm2708", "spidev"]},
...     "UART": {"enabled": False, "gpio": {14:"TX", 15:"RX"}},
...     "ONEWIRE": {"enabled": False, "gpio": {4:"DATA"}, "modules": ["w1-gpio"], "wait": 2}
... }
>>> type(FUNCTIONS)
<type 'dict'>
>>> type(FUNCTIONS['I2C'])
<type 'dict'>
>>> type(FUNCTIONS['I2C']['modules'])
<type 'list'>