最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(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ū)別的評論 (共 條)

分享到微博請遵守國家法律
安宁市| 宁乡县| 牟定县| 安远县| 宁陵县| 灵武市| 万安县| 会东县| 微博| 磐安县| 丰顺县| 南宫市| 和平县| 临西县| 南木林县| 新化县| 山阳县| 隆林| 明星| 平和县| 开封市| 嘉鱼县| 涟水县| 嫩江县| 兰溪市| 扶沟县| 大名县| 蒙阴县| 江川县| 罗定市| 临湘市| 峡江县| 万山特区| 于田县| 来宾市| 舞阳县| 肇源县| 吐鲁番市| 垣曲县| 临漳县| 讷河市|