位置参数和关键字参数小记
生活随笔
收集整理的這篇文章主要介紹了
位置参数和关键字参数小记
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
def show(a1,a2,a3):passshow(1,2,3) 位置參數(shù)show(a1=1,a3=3,a2=2) 關(guān)鍵字參數(shù)
pass
位置參數(shù)示例 from booktest import views from django.urls import path, re_pathurlpatterns = [path('', views.index),re_path(r'^(\d+)$', views.detail),re_path(r'^(\d+)/(\d+)/(\d+)/$', views.detail), # 位置參數(shù) ]from django.http import HttpResponse def detail(request, p1, p2, p3): # 位置參數(shù)
return HttpResponse('year:%s-month:%s-day:%s' % (p1, p2, p3)) # 盡管輸入是數(shù)字,但是傳過來是字符串
?
def show(*args, **kwargs): # *args元組 **kwargs字典pass
位置參數(shù)示例 from booktest import views from django.urls import path, re_pathurlpatterns = [path('', views.index),re_path(r'^(\d+)$', views.detail),re_path(r'^(\d+)/(\d+)/(\d+)/$', views.detail), # 位置參數(shù) ]from django.http import HttpResponse def detail(request, p1, p2, p3): # 位置參數(shù)
return HttpResponse('year:%s-month:%s-day:%s' % (p1, p2, p3)) # 盡管輸入是數(shù)字,但是傳過來是字符串
輸出:http://127.0.0.1:8000/booktest/1/2/3/
year:1-month:2-day:3
?
關(guān)鍵字參數(shù)示例
from booktest import views from django.urls import path, re_pathurlpatterns = [path('', views.index),re_path(r'^(\d+)$', views.detail),re_path(r'^(?P<p2>\d+)/(?P<p1>\d+)/(?P<p3>\d+)/$', views.detail), # 關(guān)鍵字參數(shù) ] # ?P<指定名稱>from django.http import HttpResponsedef detail(request, p1, p2, p3): return HttpResponse('year:%s-month:%s-day:%s' % (p1, p2, p3))輸出:http://127.0.0.1:8000/booktest/1/2/3/
year:2-month:1-day:3
轉(zhuǎn)載于:https://www.cnblogs.com/gaota1996/p/10435274.html
總結(jié)
以上是生活随笔為你收集整理的位置参数和关键字参数小记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Luogu4022 CTSC2012 熟
- 下一篇: 【flutter】学习之路(一)环境的搭