自定義函式 巢狀

2021-09-24 01:37:55 字數 1403 閱讀 4317

目錄

函式巢狀的優點

保證內部函式的隱私。內部函式只能被其外部函式呼叫和訪問,不會暴露到全域性變數。

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

# 連線資料庫優化

from sqlalchemy import create_engine

import configparser, os

def connectdb():

def get_db_configuration():

path = r''

if os.path.exists(path):

conf = configparser.configparser()

conf.read(path)

# 資訊獲取

scott = conf.get('sql server', 'accountname')

tiger = conf.get('sql server', 'password')

hostname = conf.get('sql server', 'ip')

port = conf.get('sql server', 'port')

dbname = conf.get('sql server', 'dbname')

return scott, tiger, hostname, port, dbname

else:

raise filenotfounderror('配置檔案異常')

conn = create_engine('mssql+pymssql://%s:%s@%s:%s/%s' % get_db_configuration())

return conn

if __name__ == '__main__':

engine = connectdb()

合理的巢狀可以提高執行效應,如下列遞迴中,提前進行引數檢查,只需檢查一次即可。

def factorial(input):

# validation check

if not isinstance(input, int):

raise exception('input must be an integer.')

if input < 0:

raise exception('input must be greater or equal to 0' )

...def inner_factorial(input):

if input <= 1:

return 1

return input * inner_factorial(input-1)

return inner_factorial(input)

print(factorial(5))

自定義函式 Excel之自定義函式

在excel中,當系統函式不能滿足我們的需求時候,我們可以使用vba自定義函式,如抓取網頁資料,翻譯詞彙,手機號歸屬地查詢等。下面將介紹2個自定義函式,idymd函式 身份證年月日性別 通過身份證號,返回性別,出生年月日。語法 idymd id 引數 id,身份證號,預設身份證長度18位。vba 如...

自定義函式

使用者自定義函式是sqlserver的資料庫物件,他不能應用於一系列改變資料庫狀態的操作。但它可以像系統函式那樣在查詢中或儲存過程中等中的程式段中使用。也可以像儲存過程一樣通過execute命令來執行,使用者自定義函式中儲存了transact sql可以返回一定的值。在sqlserver中根據函式返...

自定義函式

自定義函式有標量值函式和錶值函式。標量值函式 如果返回結果指定一種資料型別,則函式為標量值函式。錶值函式 如果返回結果指定table則函式為表值函式。基本語法示例 標量值函式 create function funadd a int return int asbegin declare b int ...