1. 程式人生 > 其它 >FastAPI 學習之路(十四)響應模型

FastAPI 學習之路(十四)響應模型

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:woshinidaye
'''
函式!function
幾種程式設計方法
1、面向物件:類(class)、
2、面向過程:過程(def)
3、函數語言程式設計:函式(def)
'''

'''
#定義一個------函式
def test(x):            #函式名
    #this is a test function       #描述
    x = x+1         #程式碼塊
    return x        #定義返回值,並終止函式
print(test(2))

#定義一個------過程:過程是沒有返回值的函式
def test2():
    #this is a test function
    print('for process')
print('====>>>',test2())    #test2沒有定義返回值,python返回none

#函式的運用,可以把某一段重複性的功能用函式實現,這樣實現了程式碼的重複利用
#而且程式碼的修改可以直接在函式上進行修改,
import time
def log():
    #time_format = '%Y-%m-%d %X'
    time_current = time.strftime('%z-%Y-%m-%d %X')
    with open('log.txt','a+',encoding='utf-8') as f:
        f.write('%s\ttest line\n'%time_current)
def test_log_1():
    print('function test_log_1')
    #time.sleep(3)
    log()
def test_log_2():
    print('function test_log_2')
    #time.sleep(3)
    log()
test_log_1()
test_log_2()
''' ''' def test(): print('this is test function!') return 0 print('還對列印這一行嗎??') #前面是return 0,說明函式已經介紹,並返回0,這一行將不會被執行 test() print(test()) x = test() #return的值可以賦予x print('====>>>',x) ''' ''' #return的型別 def test1(): print('test1') def test2(): print('test2') return 0 def test3(): print('test3') return 1,'hello world',['zhangsan','lisi'];{'zhangsan':'lisi'} x1 = test1() #沒定義return,直譯器顯示返回0 x2 = test2() #return 0 x3 = test3() #返回元組 print(x1,'\n',x2,'\n',x3,'\n',x3[1])
''' #定義有引數的函式 ''' def test(x,y): print(x) print(y) test(1,2) #形參x、Y、實參1、2。形參的數量=實參的數量 #位置引數呼叫,與形參一一對應 test(x=2,y=2) #關鍵字呼叫,與形參書序無關 #關鍵引數不能寫在位置引數前面!!!!!!! #test(x=1,2) #positional argument follows keyword argument test(3,y=4) #可以正常執行,3賦值給x #test(3,x=3) #test() got multiple values for argument 'x' def test1(x,y,z): print(x) print(y) print(z) test1(1,2,3) print('>>>>>>><<<<<<<') test1(1,z=3,y=1)
''' #預設引數:用於預設安裝、連線資料庫的埠號 #def test1(x,y=2,z): #SyntaxError: non-default argument follows default argument,預設引數後面只能跟預設引數 '''def test1(x,y,z=2): #SyntaxError: non-default argument follows default argument,預設引數後面只能跟預設引數 print(x) print(y) print(z) test1(1,2,3) test1(1,2,z=2) test1(1,2)''' #除開預設引數,要求形參的個數與實參的個數相等,但是如何實現任意多個實參,函式都能正常執行呢? #引數組 #*args,接收N個位置引數,轉為元組 ''' def test2(*args): print(args) test2(1,2,2,2,3,4,4,4) test2(1,2,3,4,213,5,6,7,8,9,0,12) #結果會把實參全放到元組中 test2(*[1,2,3,4,213,5,6,7,8,9,0,12]) #結果會把實參全放到元組中 *[1,2,3,4,213,5,6,7,8,9,0,12] = *args args=tuple([1,2,3,4,213,5,6,7,8,9,0,12]) ''' ''' def test3(x,y,z=3,*args): print(x) print(y) print(z) print(args) test3(1,2) #結果,x=1 y=2 z=3 元組為空 test3(1,2,4) #結果,x=1 y=2 z=4 元組為空 test3(1,2,4,[12313,234,567]) #把[12313,234,567]這個列表作為一個合在一起的引數,等於是傳入了一個引數 test3(1,2,4,*[12313,234,567]) #把args=[12313,234,567]這個列表先轉換為tuple,之後再傳入,等於是傳入了三個引數 a = [12313] b = tuple([12313,234,567]) print(a,type(a)) print(b,type(b)) ''' #**kwargs,接收N個關鍵字引數,轉為字典 ''' def test3(x,y,z=3,**kwargs): #把關鍵字引數轉化為字典 print(x) print(y) print(z) print(kwargs) print(kwargs['name']) test3(1,2,name='zhangsan',age=123) test3(1,2,3,name='zhangsan',age=123) test3(1,2,3,**{'name':'zhangsan','age':123}) def test3(x,y,z=3,**kwargs): #把關鍵字引數轉化為字典 print(x) print(y) print(z) print(kwargs) print(kwargs['name']) logger('test3') def logger(source): print('from %s' % source) test3(1,2,3,name=123,age=18) ''' #區域性變數 # age = 123 #全域性變數,強烈不建議在函式內部修改全部變數 # name = 'zhangsan' # def change_name(name): # #global age # print('before change,your name is %s,age is %s ' % (name,age)) #函式裡面也可以直接引導全域性變數 # #age = 12345678 #要從函式內部修改全域性變數,需要新增global命令 # name = 'lisi' #這裡就是區域性變數,這個變數的作用域僅存在與這個函式。。。 # print('after change,your name is %s' %name) # # change_name(name) # print(name) #結果還時張三 # print(age) # names = ['zhangsan','lisi','laowang'] # def change_names(): # names[0] = 'mytest' # print('===>',names) # change_names() # print(names) #在函式裡面居然改了,全域性也生效了!! #這是因為列表、字典、集合、類,均可以在區域性,對全部修改。除了字串和整數,不能在區域性修改 #遞迴函式 #自己呼叫自己,類似於貪吃蛇, # 必須有明確的結束條件maximum recursion depth exceeded while calling a Python object #每一次遞迴的問題引數逐層減少 #遞迴函式的處理速度較慢 # def sum(n): # print(n) # return sum(n) # sum(10) # def calc(n): # print(n) # if int(n/2) >= 1: # return calc(int(n/2)) # print('====>>>',n) #上面的遞迴函式執行完了,才會執行到這一步 # a = 1.231412 # print(a,type(a),int(a),type(int(a))) # calc(100) #高階函式:將一個函式以引數的形式傳給另外一個函式,這個函式就成為高階函式 def cal(x,y,f): # c = x + y # print(c) # return c return f(x)+f(y) # cal(1,2) test = cal(3,-8,abs) # print(cal(1,2)) # print(abs(123-1234)) print(test)