国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

classonlymethod和classmethod區(qū)別

2022-11-06 21:36 作者:限量版范兒  | 我要投稿

如果要使用classonlymethod ,則需要先定義好一個classonlymethod 類。

首先我們需要明白無論是classonlymethod還是classmethod,本質(zhì)都是一個類,而classonlymethod繼承了classmethod。
classonlymethodz作用:只能被類調(diào)用,不能被實例對象調(diào)用。

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 ? ?# ?非綁定方法,靜態(tài)方法 ? ?def func1(x,y): ? ? ? ?print(x+y) f = Foo('egon') print(f,'對象f') def func3():print('這是函數(shù)func3') print(Foo.func,'函數(shù)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/面向?qū)ο?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'>> 函數(shù)func <class '__main__.Foo'> 這是classmethod -----------1----------- @classonlymethod名字是<function Foo.tell at 0x000002282392C8C8> __main__ @classonlymethod名字是<function Foo.tell at 0x000002282392C8C8> 123 page你好啊 ---------2-------------

寫入自己的博客中才能記得長久

鏈接:https://www.dianjilingqu.com/602617.html

classonlymethod和classmethod區(qū)別的評論 (共 條)

分享到微博請遵守國家法律
河北区| 武强县| 清水河县| 饶河县| 福贡县| 顺昌县| 资源县| 邵武市| 荥阳市| 阜阳市| 太保市| 兰溪市| 会东县| 东台市| 巴东县| 来凤县| 五常市| 莆田市| 揭西县| 天水市| 苏尼特右旗| 鄂托克旗| 江都市| 邢台县| 驻马店市| 桐梓县| 奉化市| 文成县| 东宁县| 曲阜市| 青冈县| 剑川县| 玉山县| 长葛市| 锡林浩特市| 晋中市| 亚东县| 天台县| 密山市| 丹阳市| 遵化市|