用类模拟C风格的赋值+返回值

这个方法比较好:

class DataHolder:
    def __init__(self, value=None):
        self.value = value
    def set(self, value):
        self.value = value
        return value
    def get(self):
        return self.value

dh = DataHolder()
if dh.set(something()):
  # do something with dh.value
elif dh.set(somethingelse()):
  # ...

参考链接:

http://stackoverflow.com/questions/1513436/what-should-i-use-instead-of-assignment-in-an-expression-in-python

http://code.activestate.com/recipes/66061/

posted @ 2013-10-31 00:19  LisPythoniC  阅读(210)  评论(0编辑  收藏  举报