About python lead pack problem

* Discussion Using fuzzy variables introduced or introduced alone will generate different objects in different files
.A 
└── Mypackage 
    ├── A.Py 
    ├── B.Py 
    ├── C.Py
 

b.py follows

Import C 

DEF B ():
     Print ( " B Start Method " )
     Print (c.config)
     Print (ID (c.config))

c.py follows

config = "ONCE"

a.py follows

import b
import c

def a():
    print(c.config)
    c.config = "TWO"
    print(c.config)
    print(id(c.config))
    print("a方法执行完毕")

a()
b.b()

Execution a.py, output

ONCE 
TWO
 2,492,322,048,184 
A method completes 
b method begins 
TWO
 2,492,322,048,184

id same value, this case means that a, b share a file objects

=========================== I am dividing line =================== ========

but. . . if

======a.py======
import b
from c import config

def a():
    global config
    print(config)
    config = "TWO"
    print(config)
    print(id(config))
    print("a方法执行完毕")

a()
b.b()
======b.py====== 
from c import config

def b():
    print("b方法开始")
    print(config)
    print(id(config))

======c.py====== 
config = "ONCE"

So this time a.py output

ONCE 
TWO
 1813891082424 
A method completes 
b method begins 
ONCE
 1,813,894,255,144

Id different values, means not the same object

 

So if you need to modify the configuration file using the global variables need to import a file name mode, which is used as a global class

Fuzzy introduced alone or introduced will have different reference types and id

 

Guess you like

Origin www.cnblogs.com/7134g/p/11546582.html
Recommended