我已經for語句如下:添加條件爲語句如果不是第一次運行
for user in tweepy.Cursor(api.followers, screen_name=s, cursor=current_cursor).items():
但是我不需要, cursor=current_cursor
將通過在第一次運行跑去。
我能想到這樣做的唯一方法是使用if語句的兩個單獨語句。
有沒有一個不太馬虎的做法呢?
我已經for語句如下:添加條件爲語句如果不是第一次運行
for user in tweepy.Cursor(api.followers, screen_name=s, cursor=current_cursor).items():
但是我不需要, cursor=current_cursor
將通過在第一次運行跑去。
我能想到這樣做的唯一方法是使用if語句的兩個單獨語句。
有沒有一個不太馬虎的做法呢?
if first_time:
first_time = False
cursor = tweepy.Cursor(api.followers, screen_names)
else:
cursor = tweepy.Cursor(api.followers, screen_name=s, cursor=current_cursor)
for item in cursor.items():
whatever(item)
初始化first_time
到True
適當的地方。
使用'current_cursor is None'來表示第一次可能就足夠了,此時'Cursor'構造函數可以移出if – Eric
tweepy.Cursor(api.followers, screen_name=s, cursor=None if first_time else current_cursor)
有可能,沒有。如果有很多這樣的參數,在函數調用中使用'** kwargs'可以更容易。但顯然不是你的情況。 – Netch
「第一次運行」是什麼意思? – WKPlus