Python-- entry function (c)

First, variable scope

When the program defines a variable that is its scope, the scope of variables called variable scope. Depending on the position variables, divided into two types:

  • Local variables: the local variable is defined in the function variables, including parameters, local variables are, after leaving the local function, can not be accessed.
  • Global variables: not within the definition of the function, globally defined variables, are global variables, global variables may be accessed in all functions.

In Python, a function to get the three tools within a predetermined range and a value of a variable dictionary.

  • globals (): Returns the dictionary the current scope of global variables; wherever use, will acquire a global variable.
  • locals (): returns the current scope of the dictionary contains local variables; when used in the global scope, the dictionary will acquire all variables globally thereof.
  • vars (): when there is no argument, equivalent to about locals (); there is a parameter, corresponding to object .__ dict__.

When using globals () and locals () global variables should not be modified, changes will alter the global variable itself. The locals () getting a local variable, even if modified, nor will it affect local variables.

Global () 例:

a = 1
def test():
	b = 2
	print (Global ())
test() # 打印全局变量 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 1, 'test': <function test at 0x0000000002EAC1E0>} globals() # 打印全局变量 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 1, 'test': <function test at 0x0000000002EAC1E0>}

locals(),例:

a = 1
def test():
	b = 2
	print (locals())

test()
# Print local variables { 'b': 2}

print (locals())
# 打印全局变量 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 1, 'test': <function test at 0x0000000002EAC1E0>}  

vars (), for example:

class test01:
	k1 = 1
	def test02():
		k2 = 2
		print (vars ())
C3 = 3

test01.test02()
# Print Test02 () local variables { 'k2': 2}

print (vars ())
# 打印全局变量 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'test01': <class '__main__.test01'>, 'k3': 3}

print (whose (test01))
# 打印类的属性 {'__module__': '__main__', 'k1': 1, 'test02': <function test01.test02 at 0x00000000023892F0>, '__dict__': <attribute '__dict__' of 'test01' objects>, '__weakref__': <attribute '__weakref__' of 'test01' objects>, '__doc__': None}

print (test01.__dict__)
# 打印类的属性 {'__module__': '__main__', 'k1': 1, 'test02': <function test01.test02 at 0x00000000023892F0>, '__dict__': <attribute '__dict__' of 'test01' objects>, '__weakref__': <attribute '__weakref__' of 'test01' objects>, '__doc__': None}

 

Although the case of global variables may be accessed by all functions, but if the global variable defined variable with the same name in the function, local variables will occur shielding global variables, for example:

a = 1
def test():
	print (a)

test()
# Run successfully, print 1


def test02():
	a = 2
	print (a)

test02()
# Internal function of the variable does not exist assignment will redefine a new local variable, print 2


def test03():
	print (a)
	a = 3

test03()
# Error UnboundLocalError: local variable 'a' referenced before assignment, since a = 3 code redefined local variables, a global variable is masked.

 

Second, use the global statement to declare a global variable in the function 

In order to avoid a global variable assignment in a function, global statement can declare global variables.

Example:

a = 1
def test():
  # Declare a global variable, the latter statement will not re-define local variables
  overall to
  print (a) # 1 Print
  # Global variable assignment    
  a = 2

test()
# 1 Print

print (a)
# 2 Print 

  

Guess you like

Origin www.cnblogs.com/mingmingming/p/11079743.html