call.py 678 字节
Newer Older
F
feilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 函数调用
# 描述:使用函数设计一组 “容器API”:init/set/get/exist

def init():
    return {}


def set(dict, key, value):
    dict[key] = value


def get(dict, key):
    return dict.get(key)


def exist(dict, key):
    return dict.get(key) is not None


def test():
    dict = init()
    for i in range(10):
        key = "key_{}".format(i)
        value = i
        set(dict, key, value)

    test_key = "key_4"
    if exist(dict, test_key):
        test_value = get(dict, test_key)
        print("key is: {}, value is: {}".format(test_key, test_value))


if __name__ == '__main__':
    test()