Development of Python variables in _python: global and local variables

If you are in for a variable in Python: global and local variables of a headache, I think this blog will help you.

Effect of operation:

Part of the code:

 1 #Variables in Python: global and local variables
 2 
 3 #In many languages, when declare global variables, like the names of global variables is defined as capital
 4 
 5 #The definition of the number of students is 100
 6 STUDENT_NUMBER = 100
 7 #The name of the school
 8 SCHOOL_NAME = 'Guangzhou University'
 9 
10 """\
11     To obtain the global variables: the number of students the value of STUDENT_NUMBER
12 """
13 def getStudentNumber():
14     return STUDENT_NUMBER
15 """\
16     Reset the global variables: the number of students STUDENT_NUMBER
17 """
18 def setStudentNumber(num):
19     global STUDENT_NUMBER
20     STUDENT_NUMBER = num
21 """\
22     To obtain the global variables: school name, the value of SCHOOL_NAME
23 """
24 def getSchoolName():
25     return SCHOOL_NAME
26 """\
27     Reset the global variables: school name, SCHOOL_NAME value
28 """
29 def setSchoolName(name):
30     global SCHOOL_NAME
31     SCHOOL_NAME = name
32 
33 def changeValue():
34     name = 'Guangzhou University'
35     number = 90
36     print('The global variable STUDENT_NUMBER = ' , getStudentNumber())
37     print('The global variable SCHOOL_NAME = ' , getSchoolName())
38     print('#######################################')
39     print('The local variable namber = ' , number)
40     print('The local variable name = ' , name)
41     print('#######################################')
42     print('The change in global variables...')
43     print('#######################################')
44     setStudentNumber(number)
45     setSchoolName(name)
46     print('The global variable STUDENT_NUMBER = ' , getStudentNumber())
47     print('The global variable SCHOOL_NAME = ' , getSchoolName())
48 
49 if __name__ == '__main__':
50     changeValue()

Posted by Chasel at November 24, 2013 - 7:05 AM