簡體   English   中英

我一直收到Nonetype,但我不認為應該嗎?

[英]I keep getting Nonetype but I dont think I should be?

我正在為項目以及json模塊使用sqlalchemy-flask。

我有兩個要從中提取數據的類。

兩種數據類型是Ints和List(我通過使用類型確保了這一點)。 當我嘗試將int追加到列表時,我得到None。 怎么了?

def update_retweet_count(TWEET, TWEET_has_retweet):

    if type(json.loads(TWEET_has_retweet.js_rt)) != list:
        list_of_retweets = list([0])
    else:
        list_of_retweets = list(json.loads(TWEET_has_retweet.js_rt))

    new_rtc = int(TWEET.tmp_rt_count)

    x = list_of_retweets.append(new_rtc)
    print x 

當我在上面運行時,X為None。

4小時后,我在下面嘗試了此方法,它起作用了!

def update_retweet_count(TWEET, TWEET_has_retweet):
    if type(json.loads(TWEET_has_retweet.js_rt)) != list:
        list_of_retweets = list([0])
    else:
        list_of_retweets = list(json.loads(TWEET_has_retweet.js_rt))

    lst =[]

    new_rtc = int(TWEET.tmp_rt_count)

    [lst.append(y) for y in list_of_retweets]

    lst.append(new_rtc)

    print lst

為什么第一個代碼不起作用?

謝謝!

費爾南多

list.append始終返回None :它將在適當位置更改列表。 你做:

x = list_of_retweets.append(new_rtc)
print x

在第一個示例中:

lst.append(new_rtc)
print lst

在第二個(正確)示例中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM