Assigning of multiple variables to a function
本问题已经有最佳答案,请猛点这里访问。
我有一些我不明白的东西。我的意思是,这是可行的,但我想知道幕后发生了什么:
1 2 3 4 5 | def load_data(): f = gzip.open('mnist.pkl.gz', 'rb') training_data, validation_data, test_data = pickle.load(f, encoding="latin1") f.close() return (training_data, validation_data, test_data) |
所以,我想了解的是,在第3行中,有3个变量(培训数据、验证数据和测试数据)。不过,它们被分配给一个函数!?我的问题是,它们和/或功能发生了什么?函数执行了三次吗?或者变量只是一个可以调用的函数?或者加载的数据被分成三部分,其中三分之一被分发到每个变量?我是个笨蛋,所以这可能很愚蠢,但请帮帮我!
(P)This is called sequence unpacking.You can find it in Python documentation,where it says:(p)布尔奇1(P)This feature of the language makes it easy to write functions which seem to return multiple results(although they actually just return a tuple),E.G.(p)字母名称
(P)No the function is not performed three times,但在同一时间返回价值。(p)(P)这个问题可以帮助你。(p)
(P)字母名称0执行11。The construct you see is called unpacking and is more or less equivalent to(p)字母名称(P)(Actually unpacking does one more thing:it assures that EDOCX1 commercial 1 has exactly 3 elements)does this help?(p)