signed

QiShunwang

“诚信为本、客户至上”

Python入门到实战(五)基础补充:安装Anaconda、ipython、装饰器、arg和kwargs、

2021/4/26 13:22:06   来源:

Python入门到实战(五)基础补充:安装Anaconda、ipython、装饰器、arg和kwargs、

  • Python基础补充
    • Anaconda
    • ipython
    • 装饰器(补笔记二)
      • 简单的装饰器
      • *arg和**kwargs
      • 报错处理IndentationError: unindent does not match any outer indentation level
      • 带参数的装饰器
      • 类装饰器

Python基础补充

Anaconda

Anaconda是继承了python以及常用的第三方库此外还提供conda
conda是一个开源的包、环境管理器,可以用于在同一个机器上安装不同版本的软件包及其依赖,并能够在不同的环境之间切换。从官网进行下载安装即可(https://www.anaconda.com/)

ipython

iPython 是一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函数。
安装方式很简单,pip install ipython即可
在安装时,始终报错Error Traceback (most recent call last)
后来我进入了Anaconda prompt进行安装就可以了

(base) C:\Users\ASUS>conda install -c anaconda ipython
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
  environment location: C:\software\Anaconda
  added / updated specs:
    - ipython

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2020.10.14 |                0         159 KB  anaconda
    conda-4.10.1               |      py3***32_1         2.9 MB
    openssl-1.1.1h             |       he7***22_0         5.8 MB  anaconda
    ------------------------------------------------------------
                                           Total:         8.8 MB

The following packages will be UPDATED:

  conda                                4.9.2-py38haa***32_0 --> 4.10.1-py38h***532_1

The following packages will be SUPERSEDED by a higher-priority channel:

  ca-certificates                                 pkgs/main --> anaconda
  openssl                                         pkgs/main --> anaconda

Proceed ([y]/n)? y

Downloading and Extracting Packages
conda-4.10.1         | 2.9 MB    | ############################################################################ | 100%
ca-certificates-2020 | 159 KB    | ############################################################################ | 100%
openssl-1.1.1h       | 5.8 MB    | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

(base) C:\Users\ASUS>
(base) C:\Users\ASUS>ipython
Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

装饰器(补笔记二)

简单的装饰器

可以让装饰器的函数在不修改任何代码的情况下添加额外的功能、
如下代码

def log(func):#定义装饰器
    def wrapper(*args,**kwargs):
        print('this is a log')
        return func(*args,**kwargs)
    return wrapper

@log
def func():#定义方法
    print('i am a function')

func()

结果

this is a log
i am a function

装饰器的本质就是调用函数,为什么这样说?看下面一段代码:

def log(func):
    def wrapper(*args,**kwargs):
        print('this is a log')
        return func(*args,**kwargs)
    return wrapper
def func():
    print('i am a function')

# func()

log(func)()

运行结果是

this is a log
i am a function

*arg和**kwargs

用来接收任意参数、只需要注意一个是对象名前*一个是**,与具体用什么名称无关,也可以是*a,**b,但是为了标准化写法、还是写*arg**kwargs
接下来看一看代码:

def f1(*args):
    print(type(args))
    print(args)
def f2(**kwargs):
    print(type(kwargs))
    print(kwargs)

f1()
f2()    

输出如下

<class 'tuple'>
()
<class 'dict'>
{}

一个接受元组,一个接受字典、这两个参数组合,可以接受任何数值

报错处理IndentationError: unindent does not match any outer indentation level

关于这个问题我存在的是缩进问题,Ctrl+A全选代码,然后shift-tab, tab、即可解决

带参数的装饰器

def log(message):
    def decorator(func):
        def wrapper(*arg,**kwarg):
            if message=='info':
                print("this is a info message")
            elif message=='error':
                print("this is a error message")
            return func(*arg,**kwarg)
        return wrapper
    return decorator

@log(message="error")
def func1():
    print("I am a func1")

@log(message="info")
def func2():
    print("I am a func2")

func1()
func2()

输出:

this is a error message
I am a func1
this is a info message
I am a func2

类装饰器

class Log(object):
    def __init__(self,func):
        self.func=func
    def __call__(self):
        print('i am log')
        self.func()
@Log
def func():
    print('I am a function')
func()
i am log
I am a function

接下来的学习内容是办公自动化,网络爬虫,未完