python函式與方法的區別

2022-05-28 21:42:14 字數 2479 閱讀 5890

一、函式和方法的區別

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

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

舉例說明:

class foo(object):

def __init__(self):

self.name="haiyan"

def func(self):

print(self.name)

obj = foo()

obj.func()

foo.func(obj)

判斷函式和方法的方式

from types import functiontype,methodtype

obj = foo()

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

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

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

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

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

三、建立表的乙個limit_choices_to引數

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

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

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

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

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

include的使用

#include匯入的相當於下面注釋的form表單的內容
inclusion_tag的使用1、建立乙個templatetags的資料夾,在裡面建立乙個change_form.py的檔案,在裡面寫**,需要加上

@register.inclusion_tag這個裝飾器

#!usr/bin/env python

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

from django.template import library

from django.urls import reverse

from stark.service.v1 import site

register = library()

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

def form(model_form_obj):

from django.forms import modelchoicefield

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

new_form =

for bfield in model_form_obj:

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

if isinstance(bfield.field, modelchoicefield):

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

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

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

print(bfield.auto_id,"111111")

dic["is_popup"] = true

dic["popup_url"] = popup_url

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

3、使用

4、stark/form.html

函式與方法的區別

函式與方法的區別 def func1 pass class a def func self pass 1.通過列印函式名的方式區別什麼是方法,什麼是函式 了解 print func1 通過類名呼叫的類中的例項方法叫做函式 print a.func 通過物件呼叫的類中的例項方法叫做方法 obj a p...

Python中函式 方法的區別

區別 定義位置 定義方式 呼叫方式 1 定義位置 函式 python的函式是直接寫在python模組中的,即在.py檔案中直接定義。方法 只能定義在class類中 2 定義方式 函式 函式定義的方式 def關鍵字 然後接函式名 再是括號 括號裡面寫形參也可以省略不寫形參 def functionna...

Scala方法與函式的區別

1.函式在scala語言中,是函式程式設計的 頭等公民 2.方法是 scala 物件導向中的概念,主要定義在類,特質,抽象類中 3.函式是乙個物件,方法本質就是類中的乙個普通的方法 4.函式可以作為引數進行傳遞,可以作為返回值返回,但是方法不行 5.方法可以通過一定的語法轉成函式 def eat 將...