2014-01-06 40 views
0

我已經for語句如下:添加條件爲語句如果不是第一次運行

for user in tweepy.Cursor(api.followers, screen_name=s, cursor=current_cursor).items(): 

但是我不需要, cursor=current_cursor將通過在第一次運行跑去。

我能想到這樣做的唯一方法是使用if語句的兩個單獨語句。

有沒有一個不太馬虎的做法呢?

+0

有可能,沒有。如果有很多這樣的參數,在函數調用中使用'** kwargs'可以更容易。但顯然不是你的情況。 – Netch

+1

「第一次運行」是什麼意思? – WKPlus

回答

0
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_timeTrue適當的地方。

+1

使用'current_cursor is None'來表示第一次可能就足夠了,此時'Cursor'構造函數可以移出if – Eric

2
tweepy.Cursor(api.followers, screen_name=s, cursor=None if first_time else current_cursor) 
相關問題