zoukankan      html  css  js  c++  java
  • python装饰器之staticmethod,classmethod,property 分类: python 20130217 17:08 1970人阅读 评论(0) 收藏

    文章出处:http://www.python123.com/thread-50-1-1.html。部分内容加以补充。


      许久之前,一直不知道staticmethod,classmethod,property能做什么用处,不过说实话,这三个装饰器的用处也不是很大~

    但是呢,偶尔还是会接触到相关的,所以还是弄清楚比较的好。


       1. 先说说staticmethod吧

      class Rabbit:
          @staticmethod
          def mystatic(wParam,lParam):
                print 'this is  a static method'
         
      这里 mystatic 静态方法,和 c++里面的类静态方法基本一样。

    It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.Static methods in Python are similar to those found in Java or C++.


    ===========================================

    另外一种非静态方法:

    class pin:
      #@staticmethod

      def pi(self): 
        return 'ok'

    va=pin()

    print va.pi()   #如果在静态方法中执行该句,则需要把pi(self)中的self去掉

    #print pin.pi()  #非静态方法中,必须创建类的实例,通过实例调用方法。如果把此注释去掉,则会报错

    ===========================================


    2.classmethod
       请先看下例子输出:
    class Rabbit(object):
        def __init__(self,name):
            self._name = name
        @classmethod
        def newClass(cls):
            return 'abc',cls,Rabbit('')

    =========================

    或者:

    r=Rabbit('am')
    print r.newClass()

    =========================
    >>> Rabbit.newClass()
    ('abc', <class '__main__.Rabbit'>, <__main__.Rabbit object at 0x0259F130>)

    仔细看下也就明白了,classmethod和普通的函数,就是把传入的self换成了cls对象,注意非任何实例。


    3.property

    细心的读者可能发现上面两个例子的类构造不一样,是的。porperty修饰器是针对新的对象方式的.
    class Rabbit(object):
        def __init__(self,name):
            self._name = name
        @classmethod
        def newClass(cls):
            return 'abc',cls,Rabbit('')
        @property
        def name(self):
            return self._name
    >>> ================================ RESTART ================================
    >>>
    >>> r = Rabbit('abc')
    >>> r
    <__main__.Rabbit object at 0x0258F130>
    >>> r.name
    'abc'
    >>> r.name = '1'

    Traceback (most recent call last):
      File "<pyshell#25>", line 1, in <module>
        r.name = '1'
    AttributeError: can't set attribute
    说白了,就是给函数设置只读属性。
    这里定义的属性是一个只读属性,如果需要可写,则需要再定义一个setter:
    class Rabbit(object):
        def __init__(self,name):
            self._name = name
        @classmethod
        def newClass(cls):
            return 'abc',cls,Rabbit('')
        @property
        def name(self):
            return self._name
        @name.setter
        def name(self,newname):
            self._name= newname

    另外说下:如果类定义的时候是 class Rabbit: ...这样的话,这个@property是米有只读限制的。建议大家都用新的类定义风格吧~


  • 相关阅读:
    标准差、方差、协方差的简单说明
    样本的均值和方差的无偏估计
    Network In Network——卷积神经网络的革新
    Rethinking the inception architecture for computer vision的 paper 相关知识
    VIM的自动补全
    substitute 命令与 global 命令
    两个月全马训练参照表
    初跑者秘诀
    python3入门教程
    使用Python3.x抓取58同城(南京站)的演出票的信息
  • 原文地址:https://www.cnblogs.com/think1988/p/4628240.html
Copyright © 2011-2022 走看看