python fourteenth day

python fourteenth day

  • Decorator: presents the perfect open closed principle. Essence decorators: closures.

  • def wrapper(f):
      def inner(*args,**kwargs):
          """执行被装饰函数之前的操作"""
          ret = f(*args,**kwargs)
          """执行被装饰函数之后的操作"""
          return ret
        return inner
import time
def wrapper(f):
    def inner(*args,**kwargs):
        with open('log',encoding='utf-8',mode='a') as f1:
            struct_time = time.localtime()
            f1.write(f'北京时间:{time.strftime("%Y-%m-%d %H:%M:%S",struct_time)}执行了 {f.__name__}函数\n')
        ret = f(*args,**kwargs)
        return ret
    return inner

@wrapper
def wcnb():
    print('in wcnb')
wcnb()
time.sleep(2)
wcnb()
time.sleep(1)
wcnb()

Custom modules:

What is the module: essentially .py file, the smallest unit of the package statement.

Custom modules: in fact, the definition of .py, which can include: definitions of variables, executable statement, for loop, and so the function definition, they collectively module members.

Module operating mode:

  • Script: Direct executed by an interpreter. Right or PyCharm run.
  • Module by: introducing the other modules. Resources (variables, function definitions, class definitions, etc.) introduced into it as a module.

__name__Use the property:

In the script run time, __name__fixed string:__main__

When introduced in a modular manner, __name__it is the name of this module.

In a custom module to __name__judge, to decide whether to execute executable statements: development, implementation, use phase is not executed.

System lead path module

  • Memory: If you have previously been successfully imported a module, the direct use of already existing modules
  • Built path: the installation path: Lib
  • PYTHONPATH: find the path module during import.
  • sys.path: is a list of paths.

If the above are not found on the error.

Custom modules to sys.path by modifying the sys.path dynamically.

os.path.dirname (): Get the parent path of a path. It is generally used to obtain a relative path of the current module

import sys
import os
sys.path.append(os.path.dirname(__file__) + '/aa')

Import module in various ways:

  • import xxx: all members to import a module
  • import aaa, bbb: a one-time introduction of a member of the plurality of modules. Such an approach is not recommended, write separately.
  • from xxx import a: Import a specified member from the module.
  • from xxx import a, b, c: introducing a plurality of members from the module.
  • from xxx import *: import all the members from the module.

and the difference between import xxx from xxx import * of

The first way in which members use, you must use the name of the module. Not prone to naming conflicts.

The second way in which members use, do not use the name of the module, directly member name. But prone to naming conflicts. After defining the members of the entry into force of (the front cover.)

How to solve the problem of name conflicts

  • Import xxx instead introduced in this way.
  • Avoid using the same name as their own
  • Using aliases to resolve conflicts

Use an alias: alias

To members from the alias names to avoid conflicts.

from my_module import age as a

Alias ​​to the module from an object to simplify the writing.

import my_module as m

from xxx import * control member is introduced

By default, all members will be imported.

__all__Is a list to represent the members of the module can be used outside. Are strings member name.

note:

__all__Just take effect from xxx import * import this way. The rest of the way does not take effect.

Relative imports

Import for a project between different modules, known as the relative import.

Only one format:

a relative path from import xxx

Relative path: a relative path comprises a number of points.

It indicates that the current path.

.. indicates the parent path.

... indicates the path parent parent path.

# 相对导入同项目下的模块
# from ..z import zz            # 容易向外界暴露zz模块
from ..z.zz import *
# 不使用相对导入的方式,导入本项目中的模块
# 通过当前文件的路径找到z的路径
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)) + '/z')
from zz import *

Common modules: random

This module provides a method and a random number to obtain relevant:

  • random.random (): Get a float within the range [0.0, 1.0) of
  • random.randint (a, b): obtaining an integer within [a, b] range
  • random.uniform (a, b): obtaining float within [a, b) range
  • random.shuffle (x): the parameter specifies the data elements in the upset. Parameter must be a variable data type.
  • random.sample (x, k): random data from x k, returns a list composed.

Guess you like

Origin www.cnblogs.com/styxr/p/12171046.html
Recommended