classonlymethod和classmethod区别

如果要使用classonlymethod ,则需要先定义好一个classonlymethod 类。

首先我们需要明白无论是classonlymethod还是classmethod,本质都是一个类,而classonlymethod继承了classmethod。
classonlymethodz作用:只能被类调用,不能被实例对象调用。

class classonlymethod(classmethod):  # 继承classmethod
    def __get__(self, instance, cls=None): # 
        if instance is not None:
            raise AttributeError("This method is available only on the class, not on instances.")
        return super(classonlymethod, self).__get__(instance, cls)

示例2

from django.utils.decorators import classonlymethod


class Foo:
    def __init__(self, name, a = None):
        self.name = name

    @classonlymethod
    def as_view(cls, actions=None, **initkwargs):
        """
        Because of the way class based views create a closure around the
        instantiated view, we need to totally reimplement `.as_view`,
        and slightly modify the view function that is created and returned.
        """
        # The name and description initkwargs may be explicitly overridden for
        # certain route confiugurations. eg, names of extra actions.
        cls.name = None
        cls.description = None
        page = None

        # The suffix initkwarg is reserved for displaying the viewset type.
        # This initkwarg should have no effect if the name is provided.
        # eg. 'List' or 'Instance'.
        cls.suffix = None

        # The detail initkwarg is reserved for introspecting the viewset type.
        cls.detail = None

        # Setting a basename allows a view to reverse its action urls. This
        # value is provided by the router through the initkwargs.
        cls.basename = None

        # actions must not be empty
        if not actions:
            raise TypeError("The `actions` argument must be provided when "
                            "calling `.as_view()` on a ViewSet. For example "
                            "`.as_view({'get': 'list'})`")


    def tell(self):    #  绑定到对象
        print('名字是%s'%self.name)

    @classonlymethod
    def tell2(cls, page=None):    #  绑定到对象
        print('@classonlymethod名字是%s'%cls.tell)
        if page is not None:
            print(page,'page你好啊')

    @classmethod   #  绑定到类
    def func(cls):
        print(cls,'这是classmethod')

    @staticmethod    #  非绑定方法,静态方法
    def func1(x,y):
        print(x+y)


f = Foo('egon')
print(f,'对象f')

def func3():print('这是函数func3')
print(Foo.func,'函数func')
Foo.func()
print('-----------1-----------')
Foo.tell2()
print(Foo.tell2.__module__)
Foo.tell2(123)
print('---------2-------------')
# f.tell2()
Foo.as_view(actions='get')
f.as_view(actions='get')     #
print(Foo.as_view.__module__)

输出:

Traceback (most recent call last):
  File "D:/python3.64/sss/python_note/面向对象/21_绑定方法与非绑定方法介绍.py", line 72, in <module>
    f.as_view(actions='get')     #
  File "D:\PythonVenv\drf_venv\lib\site-packages\django\utils\decorators.py", line 11, in __get__
    raise AttributeError("This method is available only on the class, not on instances.")
AttributeError: This method is available only on the class, not on instances.
<__main__.Foo object at 0x00000228236ED3C8> 对象f
<bound method Foo.func of <class '__main__.Foo'>> 函数func
<class '__main__.Foo'> 这是classmethod
-----------1-----------
@classonlymethod名字是<function Foo.tell at 0x000002282392C8C8>
__main__
@classonlymethod名字是<function Foo.tell at 0x000002282392C8C8>
123 page你好啊
---------2-------------
posted @ 2022-11-06 20:47  ty1539  阅读(121)  评论(0编辑  收藏  举报