zoukankan      html  css  js  c++  java
  • 模块简介

    Python模块自信:开放

    Python是一个开放的系统。

    1.编写模块

    >>> math.poe(3,2)
    Traceback (most recent call last):
      File "<pyshell#29>", line 1, in <module>
        math.poe(3,2)
    AttributeError: module 'math' has no attribute 'poe'
    >>> math.pow(3,2)
    9.0
    >>> 

    这里math十Python标准库中一个,用import引入,然后可以使用。

    1.1模块是程序

    模块就是一个Python程序,在需要它的时候将其引用过来。

    如果我们自己写一个文件能不能作为模块引用呢

    先命名一个Python文件’pm.py‘

    lang = 'Python'
    >>> 
     RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/pm.py 
    >>> import sys>>> sys.path.append('C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/pm.py')
    >>> import pm
    >>> pm.lang
    'Python'
    >>> 

    需要告诉Python解释器我们模块的位置(用import sys)然后就可以调用我们自定义的模块了。

     1.2模块的位置

    为了让写的模块能被Python解释器知道,需要用sys.path.append。其实,在Python中,所有可引用的模块加入到 sys.path里面了。

    >>> import sys
    >>> import pprint
    >>> pprint.pprint(sys.path)
    ['C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32',
     'C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\Lib\idlelib',
     'C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python36.zip',
     'C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\DLLs',
     'C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib',
     'C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32',
     'C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages',
     'C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/pm.py']
    >>> 

    上面位置内的.py文件都可以作为模块引入。

    1.3--al--在模块中的作用

    __all__ = ['__privata_variable','public_teacher']
    
    public_variable = '我是public_variable'
    _privata_variable = '我是privata variable'
    
    def public_teacher():
        print('我是public')
    def _privata_teacher():
        print('我是privata')
    >>> import sys
    >>> sys.path.append ('C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/pp.py')
    >>> import pp
    >>> from pp impport *
    SyntaxError: invalid syntax
    >>> from pp import *
    >>> public_variable
    '我是public_variable'
    >>> _privata_variable
    '我是privata variable'
    >>> public_teacher()
    我是public
    >>> _privata_teacher()
    我是privata
    >>> 

    1.4包和库

     顾名思义,包和库比模块大。Python标准库中的每个库都有好几个包,每个包有若干个模块。

     2标准库概述

    安装Python时默认已经安装好的模块被统称为标准库。

    2.1 引用的方式

    >>> import pprint
    >>> from pprint import pprint
    >>> from pprint import *
    >>> import pprint as pr
    >>> from pprint import pprint as pt

    2.2 帮助,文档和源码

    Python模块的属性和方法是大部分都难以记忆的,所以需要查询(dir(),help())

    >>> import pprint
    >>> print(pprint.__doc__)
    Support to pretty-print lists, tuples, & dictionaries recursively.
    
    Very simple, but useful, especially in debugging data structures.
    
    Classes
    -------
    
    PrettyPrinter()
        Handle pretty-printing operations onto a stream using a configured
        set of formatting parameters.
    
    Functions
    ---------
    
    pformat()
        Format a Python object into a pretty-printed representation.
    
    pprint()
        Pretty-print a Python object to a stream [default is sys.stdout].
    
    saferepr()
        Generate a 'standard' repr()-like value, but protect against recursive
        data structures.
    
    
    >>> dir(pprint)
    ['PrettyPrinter', '_StringIO', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_builtin_scalars', '_collections', '_perfcheck', '_recursion', '_safe_key', '_safe_repr', '_safe_tuple', '_sys', '_types', '_wrap_bytes_repr', 'isreadable', 'isrecursive', 'pformat', 'pprint', 're', 'saferepr']
    >>> help(pprint)
    Help on module pprint:
    
    NAME
        pprint - Support to pretty-print lists, tuples, & dictionaries recursively.
    
    DESCRIPTION
        Very simple, but useful, especially in debugging data structures.
        
        Classes
        -------
        
        PrettyPrinter()
            Handle pretty-printing operations onto a stream using a configured
            set of formatting parameters.
        
        Functions
        ---------
        
        pformat()
            Format a Python object into a pretty-printed representation.
        
        pprint()
            Pretty-print a Python object to a stream [default is sys.stdout].
        
        saferepr()
            Generate a 'standard' repr()-like value, but protect against recursive
            data structures.
    
    CLASSES
        builtins.object
            PrettyPrinter
        
        class PrettyPrinter(builtins.object)
         |  Methods defined here:
         |  
         |  __init__(self, indent=1, width=80, depth=None, stream=None, *, compact=False)
         |      Handle pretty printing operations onto a stream using a set of
         |      configured parameters.
         |      
         |      indent
         |          Number of spaces to indent for each level of nesting.
         |      
         |      width
         |          Attempted maximum number of columns in the output.
         |      
         |      depth
         |          The maximum depth to print out nested structures.
         |      
         |      stream
         |          The desired output stream.  If omitted (or false), the standard
         |          output stream available at construction will be used.
         |      
         |      compact
         |          If true, several items will be combined in one line.
         |  
         |  format(self, object, context, maxlevels, level)
         |      Format object for a specific context, returning a string
         |      and flags indicating whether the representation is 'readable'
         |      and whether the object represents a recursive construct.
         |  
         |  isreadable(self, object)
         |  
         |  isrecursive(self, object)
         |  
         |  pformat(self, object)
         |  
         |  pprint(self, object)
         |  
         |  ----------------------------------------------------------------------
         |  Data descriptors defined here:
         |  
         |  __dict__
         |      dictionary for instance variables (if defined)
         |  
         |  __weakref__
         |      list of weak references to the object (if defined)
    
    FUNCTIONS
        isreadable(object)
            Determine if saferepr(object) is readable by eval().
        
        isrecursive(object)
            Determine if object requires a recursive representation.
        
        pformat(object, indent=1, width=80, depth=None, *, compact=False)
            Format a Python object into a pretty-printed representation.
        
        pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False)
            Pretty-print a Python object to a stream [default is sys.stdout].
        
        saferepr(object)
            Version of repr() which can handle recursive data structures.
    
    DATA
        __all__ = ['pprint', 'pformat', 'isreadable', 'isrecursive', 'saferepr...
    
    FILE
        c:usersadministratorappdatalocalprogramspythonpython36-32libpprint.py

     

     

    https://www.cnblogs.com/serpent/
  • 相关阅读:
    缓存架构设计细节二三事
    啥,又要为表增加一列属性?
    SpringMvc4.x---快捷的ViewController
    SpringMvc4.x--@ControllerAdvice注解
    SpringMvc4.x--Spring MVC的常用注解
    解决svn--Unable to connect to a repository at URL ‘https://xxxxxx’ 问题
    或许你不知道的10条SQL技巧
    Java 基础-运算符
    Java 运算符 % 和 /
    Java基础-注释
  • 原文地址:https://www.cnblogs.com/serpent/p/7606129.html
Copyright © 2011-2022 走看看