python(1)

1.python模块导入原理

   概念:模块即 包含函数、类、变量的独立的python文件

   使用关键字 import

   a.

     import 模块名

     模块名.函数名()

     新建文件m.py

1 def plus(a, b);
2      return a + b
3 
4  def plus2();
5       jpass

     新建文件new.py

1 import m
2 
3 print m.plus(1, 2)

   b.
      from 模块名 import 函数名

      函数名()

1 from m import plus
2 
3 print plus(1, 2)


2.导入系统模块

   举例

1 import sys.path
2 
3 from os import *
4 
5 import string, re
6 import time, random
7 import socket. threading
8 
9 .....通过系统模块调用方法......


3.导入私有模块

   举例

 1 import sys
 2 
 3 #/home/mypython/modules/m.py  
 4 
 5 path =  '/home/mypython/modules/'
 6 
 7 #把模块所在目录添加到path中,方便查找加载模块
 8 
 9 sys.path.append(path)
10 
11 from m import plus2
12 
13 plus2()

 4.变量

    字典:   字典名 = {'关键字' : 值,....}

                字典名.get('关键字')  或  字典名.get('关键字',值) 不确定是否存在该关键字;

                字典名.['关键字']  = 值   //向字典某个关键字赋值 或者 追加属性

                字典对象包含的方法有: keys()、values()、 copy()、popitem() 等

                删除字典某一项的方法 del 字典名.['关键字']

                判断某一项是否在字典中: '关键字' in 字典名

5.后台以json格式返回数据

   HttpResponse(json.dumps(response_data, default=json_util.default), 'application/json')    

原文地址:https://www.cnblogs.com/yiliweichinasoft/p/3558412.html