I am using some open SNS webiste’s API, the python version is a binding to its JSON version, but I really can’t understand below, why do things like this?
def mentions(self):
comments = self.api.mentions()
for comment in comments:
self.obj = comment
mid = self.getAtt("id")
text = self.getAtt("text")
print "mentions---"+ str(mid) +":"+ text
Why not access comment’s own attribute but assign to self.obj?
Perhaps
commentisn’t amenable to direct attribute access. If so…It looks like the API was designed by a Java programmer. A more Pythonic solution — assuming you can’t access the attributes of
commentdirectly — would be to rename thegetAttmethod to__getattr__and write, e.g.,mid = self.id.But even then the idea of assigning each
commenttoself.objin turn seems perverse. Some wrapper aroundcommentwould probably be better:In fact, this would be sufficiently concise that you would even bother with local variables: