Python variable scopes and modules

Source: Internet
Author: User

Tag: Phone class exists line execution character string named span

Global variables: Global variables can be accessed throughout the program scope.

Local variable: A local variable can only be accessed within its declared function.

Globvar= 0Def Set_globvar_to_one ():  global  Globvar # use global Declaration global variable  Globvar = 1def Print_globvar  ():  print (globvar< Span class= "pun" >)  # not using Globalset_globvar_to_one () print Globvar # output 1 print_globvar ()  # output 1, Globvar in function is already a global variable  

1. Global---Define variables as globals. You can change the value of a variable within a function by defining it as a global variable.

2. A global statement can define multiple variables at the same time, such as Global X, Y, Z.

Module: The python module is a python file that ends with a. Py, containing the definition of the Python object and the Python statement.

Modules allow you to logically organize your Python code snippets.

Assigning the relevant code to a module will make your code better and easier to understand.

Modules can define functions, classes, and variables, and modules can also contain executable code.

Import Module Mode one: Import module name

Call the function inside the module: module name. Name of the function

Import Support # Importing module

Support. Print_func("runoob") # can now invoke functions contained in the module

Import Module Mode two: From the Module name import function name 1, function name 2 ....

from fib import Fibonacci to import the Fibonacci function of the module fib

This declaration does not import the entire FIB module into the current namespace, it only introduces the Fibonacci individual in the FIB to the global symbol table of the module that executes the declaration.

It is also possible to import all the contents of a module into the current namespace, just use the following declaration:

From modname import *


Reload (module name)

Module_name to directly put the module's name, not a string form. For example, to reload the Hello module, as follows:

Reload(hello)

Packages in Python:
A package is a folder, but a __init__.py file must exist under the folder, and the contents of the file can be empty. __int__.py is used to identify the current folder as a package.

# import Phone Pack
From package_runoob. Runoob1 Import runoob1
From package_runoob. RUNOOB2 Import runoob2
Runoob1()
runoob2()

Python variable scopes and modules

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.