python之路_物件導向相關知識點

來源:互聯網
上載者:User

標籤:func   url   mes   space   AC   user   屬性   ini   col   

1、方法與函數的區別?

‘‘‘class Foo(object):    def __init__(self):        self.name = ‘alex‘    def func(self):        print(self.name)from types import FunctionType,MethodTypeobj = Foo()print(isinstance(obj.func,FunctionType))             # Falseprint(isinstance(obj.func,MethodType))               # Trueprint(isinstance(Foo.func,FunctionType))             # Trueprint(isinstance(Foo.func,MethodType))               # False注意:    方法,無需傳入self參數;函數,必須手動傳入self參數‘‘‘

2、models對象相關查詢

‘‘‘例:model.UserInfo為models類‘‘‘    #(1)擷取models類所在app名:model.UserInfo._meta.app_label    #(2)擷取models類的類名小寫:model.UserInfo._meta.model_name    #(3)擷取models類中的欄位內容:model.UserInfo._meta.get_field(‘username‘)    #(4)獲models類中欄位的verbose_name:model.UserInfo._meta.get_field(‘username‘).verbose_name

3、用type建立類的實現

‘‘‘type建立TestModelForm類,參數分別為類名,繼承類,類中屬性‘‘‘

meta = type(‘Meta‘,(object,),{‘model‘:self.model_class,‘fields‘:‘__all__‘})TestModelForm = type(‘TestModelForm‘,(ModelForm,),{‘Meta‘:meta})#等價於class TestModelForm(ModelForm): class Meta: model = self.model_class fields = "__all__"

4、反向產生url

#樣本如下:namespace為名稱空間,name為url別名

reverse("namespace:name")

 

python之路_物件導向相關知識點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.