python - 在打印列表结束时得到 'none'

python - getting 'none' at the end of printing a list

这里是新手。有人可以向我解释为什么 'none' 打印在这段代码的末尾,但只有在函数内部调用时才打印吗?

背景: 我有一个包含一些列表的变量 (share_data):

share_data = 
[['Date', 'Ticker', 'Company', 'Mkt Cap', 'VM Rank', 'Value Rank', 'Momentum Rank'],  
['2016-08-27', 'BEZ', 'Beazley', '2,063', '89', '72', '76'], 
['2016-08-30', 'BEZ', 'Beazley', '2,063', '89', '72', '76'], 
['2016-08-31', 'BEZ', 'Beazley', '2,050', '89', '72', '75'], 
['2016-09-01', 'BEZ', 'Beazley', '2,039', '96', '73', '93'], 
['2016-09-02', 'BEZ', 'Beazley', '2,069', '90', '72', '77'], 
['2016-09-03', 'BEZ', 'Beazley', '2,120', '96', '70', '94'], 
['2016-09-06', 'BEZ', 'Beazley', '2,106', '90', '71', '77'], 
['2016-09-07', 'BEZ', 'Beazley', '2,085', '89', '71', '76'], 
['2016-09-08', 'BEZ', 'Beazley', '2,091', '89', '72', '77'], 
['2016-09-09', 'BEZ', 'Beazley', '2,114', '89', '71', '77'], 
['2016-09-10', 'BEZ', 'Beazley', '2,084', '94', '71', '89'], 
['2016-09-12', 'BEZ', 'Beazley', '2,084', '94', '71', '89']]

我有兴趣打印最后 5 行。

如果我在 main 程序中使用它:

for row in share_data[-5:]:
    print(row)

我得到了正确的数据:

['2016-09-07', 'BEZ', 'Beazley', '2,085', '89', '71', '76']
['2016-09-08', 'BEZ', 'Beazley', '2,091', '89', '72', '77']
['2016-09-09', 'BEZ', 'Beazley', '2,114', '89', '71', '77']
['2016-09-10', 'BEZ', 'Beazley', '2,084', '94', '71', '89']
['2016-09-12', 'BEZ', 'Beazley', '2,084', '94', '71', '89']

...但是当我创建一个函数来执行此操作时:

def share_details(share_data, n=5):
    ''' Prints the last n rows of a share's records'''
    for row in share_data[-n:]:
        print(row)
    return

并以这种方式调用函数:

print(share_details(share_data))

...我得到的是这个(注意最后的 'None'):

['2016-09-07', 'BEZ', 'Beazley', '2,085', '89', '71', '76']
['2016-09-08', 'BEZ', 'Beazley', '2,091', '89', '72', '77']
['2016-09-09', 'BEZ', 'Beazley', '2,114', '89', '71', '77']
['2016-09-10', 'BEZ', 'Beazley', '2,084', '94', '71', '89']
['2016-09-12', 'BEZ', 'Beazley', '2,084', '94', '71', '89']
None

我认为是函数末尾的'return'语句触发了它,但不知道how/why。

编辑 - 现在很清楚我的错误是什么(即打印 inside 函数,然后打印 return 值)我可以跟进吗一个额外的问题? 将所有打印委托给一个函数是好习惯吗?也许调用一个函数,以提高可读性:

print_share_details(share_data)

或者有更好的方法readable/pythonic?

Python return 中的每个函数都有默认 return 值 None。所以

print(share_details(share_data))

calls share_details, share_details 打印 share_data, share_details returns None 的最后 5 行(默认),然后 print 打印出 return 值。

print(share_details(share_data)) 将打印该方法调用的 return。在您的方法中,您只需要 return,它将 return None。简单地做:

return

Will return None,这相当于没有 return,因为 None 是由没有 [=] 的方法 return 编辑的15=].

您需要确定您想要 return 什么作为对方法调用的响应。如果你不想 return 任何东西,而只是从你的方法中打印,那么你不需要实际显式调用 return 也不需要 print 你的方法调用。

所以,与其这样做:

print(share_details(share_data))

简单地调用它而不打印:

share_details(share_data)

并删除方法末尾的 return,因此您将拥有:

def share_details(share_data, n=5):
    ''' Prints the last n rows of a share's records'''
    for row in share_data[-n:]:
        print(row)

当你写 print(share_details(share_data)) - 这意味着打印由 share_details 函数编辑的值 return。打印值后的函数 returns None

如果函数 return 没有任何需要打印的东西,最好省略 print 并像这样调用函数:

share_details(share_data)

只需删除外部打印语句。它没用,并且正在从空的 return 语句打印 None 值。