Python diary----2017.7.26

Source: Internet
Author: User

Name space
Namespaces are divided into three types
Global namespaces
Local namespaces
Built-in namespaces (the name that the Python interpreter gives us, like ' input,print ')

Load order between three namespaces:
Load order: Built-in namespaces (pre-Program load)-Global namespaces (program run: Load from top to bottom) local namespaces (program run: Load only when called)
Order of values between three namespaces:
In the local call:
Local namespaces, global namespaces,
Built-in namespaces
In the global call:
Built-in namespaces, global namespaces
Scope:
Scope is the scope of action, according to the effective scope can be divided into global scope and local scope.

Global scope:
Contains built-in namespaces, global namespaces, can be referenced anywhere in the entire file, are globally valid

Local scope:
Local namespace, only valid within local scope
The (Globals () function returns all global variables of the current position in the dictionary type. )
The (Locals () function returns all local variables of the current position in the dictionary type. )
Nesting of functions:
The nested function, as the name implies, is the function inside the nested function.
Def A ():
Print (' a ')
Def B ():
Print (' B ')
B ()
Print (' Over!!! ')
A ()
Run results
A
B
OVER!!!
The nature of the function name:
1. Can be referenced
2. Elements that can be used as container types
3. Can be used as a function parameter and return value
Closures:
An intrinsic function contains a reference to an outer scope rather than a full-action domain name, which is called a closure function
def func ():
name = ' Eva '
def inner ():
Print (name)

Homework:

# 2, write the function, the user passed in the modified file name, with the content to be modified, execute the function, complete the whole file batch modification operation
# import OS
# DEF Rep (File_name,new,old):
#
# with open (file_name, ' R ', encoding= ' Utf-8 ') as read_f,\
# open (' H ', ' W ', encoding= ' Utf-8 ') as Write_f:
# for line in Read_f:
# Write_f.write (Line.replace (New,old))
#
# Os.remove (file_name)
# os.rename (' h ', file_name)
#
# Rep (' Hello.txt ', ' * ', ' 7 ')

# 3, write functions, check whether each element of the user's incoming object (string, list, tuple) contains empty content.

# def func (obj):
# for I in obj:
# If I:
# print (' The element is not empty ', i)
# Else:
# a = Obj.index (i)
# print (' This element is empty ', i)
#
#
# func ([' A ', ' ', '])
#


# 4, write the function, check the length of each value passed in the dictionary, if greater than 2, then only the first two length of the content, and return the new content to the caller.
# dic = {"K1": "V1v1", "K2": [11,22,33,44]}
# PS: value in a dictionary can only be a string or list
# dic = {"K1": "V1v1", "K2": [11,22,33,44]}
# def Value_len (Leng):
# for K,v in Leng.items ():
# # print (K,V)
# If Len (v) > 2:
# Leng[k]=v[0:2]
# # Print (Leng)
# return Leng
#
# Print (Value_len (DIC))

Python diary----2017.7.26

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.