簡體   English   中英

嵌入自定義函數並在不定義的情況下調用它們

[英]Embedding custom functions and calling them without defining

是否可以像嵌入函數一樣嵌入和調用自定義函數,而不是在每個項目中編寫它們? 那么我們可以定制 function 和內置 function 嗎?

是(有點)使用import語句
例如:
項目一:

#filename is project_1

def function_1():
    print("This has been executed from project 1")

然后在您要使用它的主文件中:

import project_1

def function_2():
    print("This has been executed from project 2")

project_1.function_1()
function_2()

OUTPUT:
這已從項目 1 執行
這已從項目 2 執行

對此的唯一限制是您必須確保 2 個(或更多)文件位於同一目錄中,否則您將無法導入它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM