函式和方法的區別

2021-10-25 08:41:55 字數 2905 閱讀 7764

一、函式和方法的區別

1、函式要手動傳self,方法不用傳

2、如果是乙個函式,用類名去呼叫,如果是乙個額方法,用物件去呼叫

舉例說明:

1 class foo(object):

2 def __init__(self):

3 self.name="haiyan"

4 def func(self):

5 print(self.name)

6 7 obj = foo()

8 obj.func()

9 foo.func(obj)

判斷函式和方法的方式

1 from types import functiontype,methodtype

2 obj = foo()

3 print(isinstance(obj.func,functiontype)) #false

4 print(isinstance(obj.func,methodtype)) #true #說明這是乙個方法

5 6 print(isinstance(foo.func,functiontype)) #true #說明這是乙個函式。

7 print(isinstance(foo.func,methodtype)) #false

二、js和jquery繫結事件的幾種方式

三、建立表的乙個limit_choices_to引數

limit_choices_to:遮蔽某些選項,只顯示某些指定的選項。例如下面的,只讓顯示部門id是1001的

1 consultant = models.foreignkey(verbose_name="課程顧問", to='userinfo', related_name='consultant',limit_choices_to=)
四、include和inclusion_tag的區別

這兩個都是處理**冗餘的,由於其他的頁面也會有這樣的功能,也要用到,我們可以吧它摘出來,在建立個資料夾寫進去。匯入進來

1 如果用include,這裡面的資料得從後端傳,

2 如果用inclusion_tag,你返回啥就會幫我們傳啥,它既有自己的功能,也有include的功能,又可以處理資料

include的使用

1 23 

4 7

8 9 #include匯入的相當於下面注釋的form表單的內容

inclusion_tag的使用1、建立乙個templatetags的資料夾,在裡面建立乙個change_form.py的檔案,在裡面寫**,需要加上

@register.inclusion_tag這個裝飾器

1 #!usr/bin/env python

2 # -*- coding:utf-8 -*-

3 from django.template import library

4 from django.urls import reverse

5 from stark.service.v1 import site

6 7 register = library()

8 @register.inclusion_tag("stark/form.html")

9 def form(model_form_obj):

10 from django.forms import modelchoicefield

11 from django.forms.boundfield import boundfield # 資料都封裝在這個類了

12 new_form =

13 for bfield in model_form_obj:

14 dic = # 每乙個bfield就是form的字段,是乙個物件

15 if isinstance(bfield.field, modelchoicefield):

16 # print(bfield.field,"popup按鈕")

17 print(bfield, type(bfield)) # 18 releated_model_name = bfield.field.queryset.model # 找到關聯的類名

21 popup_url = "%s?_popupbackid=%s" % (base_url, bfield.auto_id) #每乙個input框的id

22 print(bfield.auto_id,"111111")

23 dic["is_popup"] = true

24 dic["popup_url"] = popup_url

26 return #返回的這個form是給了"stark/form.html"它裡面的form,然後迴圈遍歷

view code

3、使用

1 

2 34

5

4、stark/form.html

126
歸類 :  django

方法和函式的區別

一句話告訴你如何區分函式與方法 函式是大家的函式,方法是 類的方法。如何區分乙個iter是設計師還是碼工。看看他是在調別人的庫,還是自己寫出jar包跟別人分享。感覺差的不是乙個級別而已啊。乙個東西,用物件呼叫的叫方法,直接調函式名的叫函式。c語言等語言裡叫函式,物件導向程式設計,函式寫到類裡邊就叫做...

函式和方法的區別

區別一所處的位置 函式是直接寫檔案中而不是class中,方法是只能寫在class中。區別二定義的方式 def functionname 這裡是函式的注釋 print 這一塊寫函式的內容 class classname super def methodname self 這裡是方法的注釋 self相當...

scala函式和方法的區別

方法函式 定義是組成類的一部分 是繼承了這些 trait 的類的物件 實現方式 def fun name 引數列表 val fun 引數列表 引數列表 可有,可無 必須有,但可以為空 需要保留 呼叫fun name 方法名意味著呼叫,無參是可以省略 fun 函式名代表函式本身,必須保留引數列表 轉換...