posts - 431,  comments - 344,  trackbacks - 0
          Python似乎很討厭修飾符,沒有常見的static語法。其靜態(tài)方法的實現(xiàn)大致有以下兩種方法:

          第一種方式(staticmethod):

          >>> class Foo:
                  str = "I'm a static method."

                  def bar():
                      print Foo.str

                  bar = staticmethod(bar)


          >>> Foo.bar()
          I'm a static method.


          第二種方式(classmethod):

          >>> class Foo:
                  str = "I'm a static method."

                  def bar(cls):
                      print cls.str

                  bar = classmethod(bar)


          >>> Foo.bar()
          I'm a static method.


          ---------------------------------------------------------------

          上面的代碼我們還可以寫的更簡便些:

          >>> class Foo:
                  str = "I'm a static method."

                  @staticmethod
                  def bar():
                      print Foo.str


          >>> Foo.bar()
          I'm a static method.


          或者

          >>> class Foo:
                  str = "I'm a static method."

                  @classmethod
                  def bar(cls):
                      print cls.str


          >>> Foo.bar()
          I'm a static method.


          OK,差不多就是這個樣子了。
          posted on 2008-11-22 10:37 周銳 閱讀(411) 評論(0)  編輯  收藏 所屬分類: Python
          主站蜘蛛池模板: 深泽县| 临邑县| 绥宁县| 安多县| 施秉县| 德州市| 五峰| 民县| 尖扎县| 鄂托克旗| 德保县| 乌审旗| 呼和浩特市| 共和县| 博客| 巩留县| 泰来县| 光泽县| 佛冈县| 贵州省| 绥宁县| 荆州市| 金湖县| 东乡县| 博客| 洱源县| 沾化县| 苏尼特左旗| 印江| 三穗县| 南城县| 三江| 抚州市| 格尔木市| 白银市| 泰安市| 剑河县| 滦平县| 通许县| 防城港市| 清涧县|