添加条件为语句如果不是第一次运行
问题描述:
我已经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
适当的地方。
+1
使用'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