1. 程式人生 > 程式設計 >python3 動態模組匯入與全域性變數使用例項

python3 動態模組匯入與全域性變數使用例項

動態匯入有兩種:

1 __main__():

f="demo.A"

aa=__main__(f)

aa.A.t()

2 import importlib:

import importlib

f="demo.A"

aa=importlib.import_module(f)

aa.t()

全域性變數使用:

global_list.py:

size=None

A.py:

from demo import global_list

global_list.size=101

from demo.B import *

t()

B.py:

from demo import global_list

def t():

global_list.size+=100

print(global_list.size)

類似的php

A.php:

$size=101

include_once "./B.php"

t();

echo $size;

B.php:

function t(){

 global $size;

 $size+=100;

 echo $size;

}

以上這篇python3 動態模組匯入與全域性變數使用例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。