我正在使用 praw 模块,我发现我的对象有时具有一个属性subreddit
,有时是一个字符串,有时是一个具有自己属性的对象。我已经使用以下方法处理了它:
for c in comments:
if isinstance(c.subreddit, str):
subreddit_name = c.subreddit
else:
subreddit_name = c.subreddit.display_name
我有两个必须这样做的功能,这真的很难看。有没有更好的方法来处理这个问题?