lambda函数用一个参数产生这个3,

时间:2015-11-07 07:18:29

标签: python

>>> b = lambda x, y = 2: x + y
>>> b( 1 )

3

我无法弄清楚这个函数如何产生3

2 个答案:

答案 0 :(得分:1)

b相当于

Try combining insert values in group of 10,000

For e.g.

INSERT INTO `cities` (`city_id`, `city_name`, `countryId`, `stateid`, `countryCode`, `latitude`, `longitude`, `zip_code`) VALUES
(2, 'Djelfa', 67, 1, 'DZA', 34.67, 3.25, ''),
(3, 'Valenza', 115, 2, 'ITA', 45.02, 8.63, '15048'),


...
...
(10000, 'Valenza', 115, 2, 'ITA', 45.02, 8.63, '15048');
INSERT INTO `cities` (`city_id`, `city_name`, `countryId`, `stateid`, `countryCode`, `latitude`, `longitude`, `zip_code`) VALUES

(10001, 'Valenza', 115, 2, 'ITA', 45.02, 8.63, '15048'),
...
(185452, 'Bosendurnbach', 17, 771, 'AUT', 48.5, 15.77, ''),
(185453, 'Môlay', 79, 937, 'FRA', 47.73, 3.94, ''),
(185454, 'Miloszyce', 183, 422, 'POL', 51.05, 17.31, ''),
(185455, 'Lovce', 212, 698, 'SVK', 48.45, 18.37, ''),
(185456, 'Winchester', 174, 74, 'NZL', -44.2, 171.28, ''),
(185457, 'Wohlde', 62, 402, 'DEU', 54.4, 9.3, ''),
(185458, 'Chiavazza', 115, 2, 'ITA', 45.58, 8.07, '');


This will definitely improve the performance

然后我想你可以理解为什么b(1)是3

答案 1 :(得分:0)

那是因为默认值设置为y

让我们看一下demo:

  • 没有默认值

    >>b = lambda x, y: x + y
    >>b()
    TypeError: <lambda>() takes exactly 2 arguments (0 given)
    >>b(1)
    TypeError: <lambda>() takes exactly 2 arguments (1 given)
    >>b(1,2)
    3
    
  • 默认值为y

    >>b = lambda x, y=1: x + y
    >>b()
    TypeError: <lambda>() takes at least 1 argument (0 given)
    >>b(1)
    2
    >>b(1,2)
    3     
    
  • 有两个默认值

    >>b = lambda x=2, y=1: x + y
    >>b()
    3
    >>b(1)
    2 
    >>b(2,2)
    4