{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "92f214fe",
   "metadata": {},
   "source": [
    "# 图模式语法-python内置函数\n",
    "\n",
    "[![下载Notebook](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_notebook.svg)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/tutorials/zh_cn/compile/mindspore_python_builtin_functions.ipynb) [![下载样例代码](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_download_code.svg)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/tutorials/zh_cn/compile/mindspore_python_builtin_functions.py) [![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/master/tutorials/source_zh_cn/compile/python_builtin_functions.ipynb)\n",
    "\n",
    "当前静态图模式支持的Python内置函数包括:`int`、`float`、`bool`、`str`、`tuple`、`list`、`dict`、`getattr`、`hasattr`、`len`、`isinstance`、`all`、`any`、`round`、`max`、`min`、`sum`、`abs`、`map`、`zip`、`range`、`enumerate`、`super`、`pow`、`print`、`filter`、`type`。图模式下内置函数的使用方法与对应的Python内置函数类似。\n",
    "\n",
    "## int\n",
    "\n",
    "功能:返回一个基于数字或字符串构造的整数对象。\n",
    "\n",
    "调用:`int(x=0, base=10)`,默认转换成十进制。\n",
    "\n",
    "入参:\n",
    "\n",
    "- `x` - 需要被转换为整数的对象,支持类型为`int`、`float`、`bool`、`str`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "- `base` - 待转换进制,只有在`x`为常量`str`的时候,才可以设置该输入。\n",
    "\n",
    "返回值:转换后的整数值。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3fa24b2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  3\n",
      "b:  3\n",
      "c:  18\n",
      "d:  10\n",
      "e:  8\n",
      "f:  -1\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func(x):\n",
    "    a = int(3)\n",
    "    b = int(3.6)\n",
    "    c = int('12', 16)\n",
    "    d = int('0xa', 16)\n",
    "    e = int('10', 8)\n",
    "    f = int(x)\n",
    "    return a, b, c, d, e, f\n",
    "\n",
    "x = ms.Tensor([-1.0], ms.float32)\n",
    "a, b, c, d, e, f = func(x)\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)\n",
    "print(\"e: \", e)\n",
    "print(\"f: \", f)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b92e0368",
   "metadata": {},
   "source": [
    "## float\n",
    "\n",
    "功能:返回一个基于数字或字符串构造的浮点数对象。\n",
    "\n",
    "调用:`float(x=0)`。\n",
    "\n",
    "入参:`x` - 需要被转换为浮点数的对象,支持类型为`int`、`float`、`bool`、`str`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "返回值:转换后的浮点数值。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3f78e866",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  1.0\n",
      "b:  112.0\n",
      "c:  -123.5999984741211\n",
      "d:  123.0\n",
      "e:  -1.0\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func(x):\n",
    "    a = float(1)\n",
    "    b = float(112)\n",
    "    c = float(-123.6)\n",
    "    d = float('123')\n",
    "    e = float(x.asnumpy())\n",
    "    return a, b, c, d, e\n",
    "\n",
    "x = ms.Tensor([-1], ms.int32)\n",
    "a, b, c, d, e = func(x)\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)\n",
    "print(\"e: \", e)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9716de8",
   "metadata": {},
   "source": [
    "## bool\n",
    "\n",
    "功能:返回一个基于输入构造的布尔值的对象。\n",
    "\n",
    "调用:`bool(x=false)`。\n",
    "\n",
    "入参:`x` - 需要被转换为布尔值的对象,支持类型为`int`、`float`、`bool`、`str`、`list`、 `tuple`、 `dict`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "返回值:转换后的布尔值。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6ee321db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  False\n",
      "b:  False\n",
      "c:  True\n",
      "d:  True\n",
      "e:  True\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = bool()\n",
    "    b = bool(0)\n",
    "    c = bool(\"abc\")\n",
    "    d = bool([1, 2, 3, 4])\n",
    "    e = bool(ms.Tensor([10]).asnumpy())\n",
    "    return a, b, c, d, e\n",
    "\n",
    "a, b, c, d, e = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)\n",
    "print(\"e: \", e)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a199d01",
   "metadata": {},
   "source": [
    "## str\n",
    "\n",
    "功能:返回一个基于输入构造的字符串的对象。\n",
    "\n",
    "调用:`str(x='')`。\n",
    "\n",
    "入参:`x` - 需要被转换为字符串的对象,支持类型为`int`、`float`、`bool`、`str`、`list`、 `tuple`、 `dict`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "返回值:输入`x`转换后的字符串。\n",
    "\n",
    "代码用例如下,其中a为空字符串:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8187c3ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  \n",
      "b:  0\n",
      "c:  [1, 2, 3, 4]\n",
      "d:  Tensor(shape=[1], dtype=Int64, value=[10])\n",
      "e:  [1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = str()\n",
    "    b = str(0)\n",
    "    c = str([1, 2, 3, 4])\n",
    "    d = str(ms.Tensor([10]))\n",
    "    e = str(np.array([1, 2, 3, 4]))\n",
    "    return a, b, c, d, e\n",
    "\n",
    "a, b, c, d, e = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)\n",
    "print(\"e: \", e)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c2335a3",
   "metadata": {},
   "source": [
    "## tuple\n",
    "\n",
    "功能:返回一个基于输入构造的元组。\n",
    "\n",
    "调用:`tuple(x=())`。\n",
    "\n",
    "入参:`x` - 需要被转换为元组的对象,支持类型为`list`、 `tuple`、 `dict`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "返回值:按照`x`的第零维度拆分得到的元组。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ba1e7105",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  (1, 2, 3)\n",
      "b:  (1, 2, 3)\n",
      "c:  ('a', 'b', 'c')\n",
      "d:  (Tensor(shape=[], dtype=Int64, value= 1), Tensor(shape=[], dtype=Int64, value= 2), Tensor(shape=[], dtype=Int64, value= 3))\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = tuple((1, 2, 3))\n",
    "    b = tuple(np.array([1, 2, 3]))\n",
    "    c = tuple({'a': 1, 'b': 2, 'c': 3})\n",
    "    d = tuple(ms.Tensor([1, 2, 3]))\n",
    "    return a, b, c, d\n",
    "\n",
    "a, b, c, d = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f96be07",
   "metadata": {},
   "source": [
    "## list\n",
    "\n",
    "功能:返回一个基于输入构造的列表。\n",
    "\n",
    "调用:`list(x=())`。\n",
    "\n",
    "入参:`x` - 需要被转换为列表的对象,支持类型为`list`、 `tuple`、 `dict`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "返回值:按照`x`的第零维度拆分得到的列表。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "06e59ed6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a_t:  [1, 2, 3]\n",
      "b_t:  [1, 2, 3]\n",
      "c_t:  ['a', 'b', 'c']\n",
      "d_t:  [Tensor(shape=[], dtype=Int64, value= 1), Tensor(shape=[], dtype=Int64, value= 2), Tensor(shape=[], dtype=Int64, value= 3)]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = list((1, 2, 3))\n",
    "    b = list(np.array([1, 2, 3]))\n",
    "    c = list({'a':1, 'b':2, 'c':3})\n",
    "    d = list(ms.Tensor([1, 2, 3]))\n",
    "    return a, b, c, d\n",
    "a_t, b_t, c_t, d_t = func()\n",
    "print(\"a_t: \", a_t)\n",
    "print(\"b_t: \", b_t)\n",
    "print(\"c_t: \", c_t)\n",
    "print(\"d_t: \", d_t)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19d432a7",
   "metadata": {},
   "source": [
    "## dict\n",
    "\n",
    "功能:用于创建一个字典。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ec892089",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  {}\n",
      "b:  {'a': 'a', 'b': 'b', 't': 't'}\n",
      "c:  {'one': 1, 'two': 2, 'three': 3}\n",
      "d:  {'one': 1, 'two': 2, 'three': 3}\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = dict()                                          # 创建空字典\n",
    "    b = dict(a='a', b='b', t='t')                       # 传入关键字\n",
    "    c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))   # 映射函数方式来构造字典\n",
    "    d = dict([('one', 1), ('two', 2), ('three', 3)])    # 可迭代对象方式来构造字典\n",
    "    return a, b, c, d\n",
    "\n",
    "a, b, c, d = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57c502be",
   "metadata": {},
   "source": [
    "## getattr\n",
    "\n",
    "功能:获取对象的属性。\n",
    "\n",
    "调用:`getattr(x, attr, default)`。\n",
    "\n",
    "入参:\n",
    "\n",
    "- `x` - 需要被获取属性的对象,可以为任意的图模式支持类型;在JIT语法支持级别选项为`Lax`时,也支持第三方库类型。\n",
    "\n",
    "- `attr` - 需要获取的属性,需要为`str`。\n",
    "\n",
    "- `default` - 可选参数。若`x`没有`attr`,则返回`default`,可以为任意的图模式支持类型;在JIT语法支持级别选项为`Lax`时,也支持第三方库类型。若未输入`default`,且`x`没有属性`attr`,则会抛出AttributeError。\n",
    "\n",
    "返回值:目标属性或者`default`。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0c4125ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  0\n",
      "b:  2\n",
      "c:  (1,)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "\n",
    "@ms.jit_class\n",
    "class MSClass1:\n",
    "    def __init__(self):\n",
    "        self.num0 = 0\n",
    "\n",
    "ms_obj = MSClass1()\n",
    "\n",
    "@ms.jit\n",
    "def func(x):\n",
    "    a = getattr(ms_obj, 'num0')\n",
    "    b = getattr(ms_obj, 'num1', 2)\n",
    "    c = getattr(x.asnumpy(), \"shape\", np.array([0, 1, 2, 3, 4]))\n",
    "    return a, b, c\n",
    "\n",
    "x = ms.Tensor([-1.0], ms.float32)\n",
    "a, b, c = func(x)\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5d553d6",
   "metadata": {},
   "source": [
    "在静态图模式下对象的属性可能会和动态图模式下有区别,建议使用`default`输入,或者在使用`getattr`前先使用`hasattr`进行校验。\n",
    "\n",
    "其中`getattr(x.asnumpy(), \"shape\", np.array([0, 1, 2, 3, 4]))`属于高阶用法,更多介绍可见[AST扩展语法(LAX级别)](https://www.mindspore.cn/tutorials/zh-CN/master/compile/static_graph.html#ast%E6%89%A9%E5%B1%95%E8%AF%AD%E6%B3%95lax%E7%BA%A7%E5%88%AB)章节。\n",
    "\n",
    "## hasattr\n",
    "\n",
    "功能:判断对象是否具有该属性。\n",
    "\n",
    "调用:`hasattr(x, attr)`。\n",
    "\n",
    "入参:\n",
    "\n",
    "- `x` - 需要被判断是否具有某属性的对象,可以为任意的图模式支持类型;在JIT语法支持级别选项为`Lax`时,也支持第三方库类型。\n",
    "\n",
    "- `attr` - 属性名,需要为`str`。\n",
    "\n",
    "返回值:布尔值,表示是否具有该属性。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8d3da0b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  True\n",
      "b:  False\n",
      "c:  True\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "from mindspore import Tensor\n",
    "\n",
    "@ms.jit_class\n",
    "class MSClass1:\n",
    "    def __init__(self):\n",
    "        self.num0 = 0\n",
    "\n",
    "ms_obj = MSClass1()\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = hasattr(ms_obj, 'num0')\n",
    "    b = hasattr(ms_obj, 'num1')\n",
    "    c = hasattr(Tensor(np.array([1, 2, 3, 4])).asnumpy(), \"__len__\")\n",
    "    return a, b, c\n",
    "\n",
    "a, b, c = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d250cfe9",
   "metadata": {},
   "source": [
    "其中`hasattr(Tensor(np.array([1, 2, 3, 4])).asnumpy(), \"__len__\")`属于高阶用法,更多介绍可见[AST扩展语法(LAX级别)](https://www.mindspore.cn/tutorials/zh-CN/master/compile/static_graph.html#ast%E6%89%A9%E5%B1%95%E8%AF%AD%E6%B3%95lax%E7%BA%A7%E5%88%AB)章节。\n",
    "\n",
    "## len\n",
    "\n",
    "功能:获取对象(字符串或者其他可迭代对象)的长度。\n",
    "\n",
    "调用:`len(sequence)`。\n",
    "\n",
    "入参:`sequence` - `Tuple`、`List`、`Dictionary`、`Tensor`、`String`以及第三方对象(例如numpy.ndarray)。\n",
    "\n",
    "返回值:序列的长度,类型为`int`。当入参是`Tensor`时,返回的是`Tensor`第零维的长度。\n",
    "\n",
    "示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8f0d5275",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_len:3\n",
      "y_len:3\n",
      "d_len:2\n",
      "z_len:6\n",
      "n_len:4\n",
      "w_len:4\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "\n",
    "z = ms.Tensor(np.ones((6, 4, 5)))\n",
    "\n",
    "@ms.jit()\n",
    "def test(w):\n",
    "    x = (2, 3, 4)\n",
    "    y = [2, 3, 4]\n",
    "    d = {\"a\": 2, \"b\": 3}\n",
    "    n = np.array([1, 2, 3, 4])\n",
    "    x_len = len(x)\n",
    "    y_len = len(y)\n",
    "    d_len = len(d)\n",
    "    z_len = len(z)\n",
    "    n_len = len(n)\n",
    "    w_len = len(w.asnumpy())\n",
    "    return x_len, y_len, d_len, z_len, n_len, w_len\n",
    "\n",
    "input_x = ms.Tensor([1, 2, 3, 4])\n",
    "x_len, y_len, d_len, z_len, n_len, w_len = test(input_x)\n",
    "print('x_len:{}'.format(x_len))\n",
    "print('y_len:{}'.format(y_len))\n",
    "print('d_len:{}'.format(d_len))\n",
    "print('z_len:{}'.format(z_len))\n",
    "print('n_len:{}'.format(n_len))\n",
    "print('w_len:{}'.format(w_len))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7cfebfe",
   "metadata": {},
   "source": [
    "其中`len(w.asnumpy())`属于高阶用法,更多介绍可见[AST扩展语法(LAX级别)](https://www.mindspore.cn/tutorials/zh-CN/master/compile/static_graph.html#ast%E6%89%A9%E5%B1%95%E8%AF%AD%E6%B3%95lax%E7%BA%A7%E5%88%AB)章节。\n",
    "\n",
    "## isinstance\n",
    "\n",
    "功能:判断对象是否为一个已知的类型。\n",
    "\n",
    "调用:`isinstance(obj, type)`。\n",
    "\n",
    "入参:\n",
    "\n",
    "- `obj` - MindSpore支持类型的一个实例。\n",
    "\n",
    "- `type` - `bool`、`int`、`float`、`str`、`list`、`tuple`、`dict`、`Tensor`、`Parameter`,或者第三方库的类型(例如numpy.ndarray)或者是一个只包含这些类型的`tuple`。\n",
    "\n",
    "返回值:`obj`为`type`的实例,返回`True`,否则返回`False`。\n",
    "\n",
    "示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3c944cde",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_is_tuple:True\n",
      "y_is_list:True\n",
      "z_is_tensor:True\n",
      "w_is_ndarray:True\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "import numpy as np\n",
    "\n",
    "z = ms.Tensor(np.ones((6, 4, 5)))\n",
    "\n",
    "@ms.jit()\n",
    "def test(w):\n",
    "    x = (2, 3, 4)\n",
    "    y = [2, 3, 4]\n",
    "    x_is_tuple = isinstance(x, tuple)\n",
    "    y_is_list = isinstance(y, list)\n",
    "    z_is_tensor = isinstance(z, ms.Tensor)\n",
    "    w_is_ndarray = isinstance(w.asnumpy(), np.ndarray)\n",
    "    return x_is_tuple, y_is_list, z_is_tensor, w_is_ndarray\n",
    "\n",
    "w = ms.Tensor(np.array([-1, 2, 4]))\n",
    "x_is_tuple, y_is_list, z_is_tensor, w_is_ndarray = test(w)\n",
    "print('x_is_tuple:{}'.format(x_is_tuple))\n",
    "print('y_is_list:{}'.format(y_is_list))\n",
    "print('z_is_tensor:{}'.format(z_is_tensor))\n",
    "print('w_is_ndarray:{}'.format(w_is_ndarray))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abbdad59",
   "metadata": {},
   "source": [
    "其中`isinstance(w.asnumpy(), np.ndarray)`属于高阶用法,更多介绍可见[AST扩展语法(LAX级别)](https://www.mindspore.cn/tutorials/zh-CN/master/compile/static_graph.html#ast%E6%89%A9%E5%B1%95%E8%AF%AD%E6%B3%95lax%E7%BA%A7%E5%88%AB)章节。\n",
    "\n",
    "## all\n",
    "\n",
    "功能:判断输入中的元素是否均为真值。\n",
    "\n",
    "调用:`all(x)`。\n",
    "\n",
    "入参:`x` - 可迭代对象,支持类型包括`tuple`、`list`、`dict`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "返回值:布尔值,如果所有元素都为`True`,则返回`True`,否则返回`False`。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1bbe918d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  True\n",
      "b:  False\n",
      "c:  False\n",
      "d:  True\n",
      "e:  False\n",
      "f:  False\n",
      "g:  True\n",
      "h:  True\n",
      "i:  False\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "from mindspore import Tensor\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = all(['a', 'b', 'c', 'd'])\n",
    "    b = all(['a', 'b', '', 'd'])\n",
    "    c = all([0, 1, 2, 3])\n",
    "    d = all(('a', 'b', 'c', 'd'))\n",
    "    e = all(('a', 'b', '', 'd'))\n",
    "    f = all((0, 1, 2, 3))\n",
    "    g = all([])\n",
    "    h = all(())\n",
    "    x = Tensor(np.array([0, 1, 2, 3]))\n",
    "    i = all(x.asnumpy())\n",
    "    return a, b, c, d, e, f, g, h, i\n",
    "\n",
    "a, b, c, d, e, f, g, h, i = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)\n",
    "print(\"e: \", e)\n",
    "print(\"f: \", f)\n",
    "print(\"g: \", g)\n",
    "print(\"h: \", h)\n",
    "print(\"i: \", i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c584d3aa",
   "metadata": {},
   "source": [
    "其中`all(x.asnumpy())`属于高阶用法,更多介绍可见[AST扩展语法(LAX级别)](https://www.mindspore.cn/tutorials/zh-CN/master/compile/static_graph.html#ast%E6%89%A9%E5%B1%95%E8%AF%AD%E6%B3%95lax%E7%BA%A7%E5%88%AB)章节。\n",
    "\n",
    "## any\n",
    "\n",
    "功能:判断输入中的元素是存在为真值。\n",
    "\n",
    "调用:`any(x)`。\n",
    "\n",
    "入参:`x` - 可迭代对象,支持类型包括`tuple`、`list`、`dict`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "返回值:布尔值,如果所有元素都为`False`,则返回`False`,否则返回`True`。元素除了0,空,`False`外都算`True`。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a3a1b4c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  True\n",
      "b:  True\n",
      "c:  False\n",
      "d:  True\n",
      "e:  True\n",
      "f:  False\n",
      "g:  False\n",
      "h:  False\n",
      "i:  True\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "from mindspore import Tensor\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = any(['a', 'b', 'c', 'd'])\n",
    "    b = any(['a', 'b', '', 'd'])\n",
    "    c = any([0, '', False])\n",
    "    d = any(('a', 'b', 'c', 'd'))\n",
    "    e = any(('a', 'b', '', 'd'))\n",
    "    f = any((0, '', False))\n",
    "    g = any([])\n",
    "    h = any(())\n",
    "    x = Tensor(np.array([0, 1, 2, 3]))\n",
    "    i = any(x.asnumpy())\n",
    "    return a, b, c, d, e, f, g, h, i\n",
    "\n",
    "a, b, c, d, e, f, g, h, i = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)\n",
    "print(\"e: \", e)\n",
    "print(\"f: \", f)\n",
    "print(\"g: \", g)\n",
    "print(\"h: \", h)\n",
    "print(\"i: \", i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "402d0772",
   "metadata": {},
   "source": [
    "## round\n",
    "\n",
    "功能:返回输入的四舍五入。\n",
    "\n",
    "调用:`round(x, digit=0)`。\n",
    "\n",
    "入参:\n",
    "\n",
    "- `x` - 需要四舍五入的值,有效类型为 `int`、`float`、`bool`、`Tensor`以及定义了魔术方法`__round__()`第三方对象。\n",
    "\n",
    "- `digit` - 表示进行四舍五入的小数点位数,默认值为0,支持`int`类型以及`None`。若`x`为`Tensor`类型,则不支持输入`digit`。\n",
    "\n",
    "返回值:四舍五入后的值。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ca60a86a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  10\n",
      "b:  10\n",
      "c:  11\n",
      "d:  10\n",
      "e: 10.00\n",
      "f: 20.00\n",
      "g: 10.20\n",
      "h: 10.10\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = round(10)\n",
    "    b = round(10.123)\n",
    "    c = round(10.567)\n",
    "    d = round(10, 0)\n",
    "    e = round(10.72, -1)\n",
    "    f = round(17.12, -1)\n",
    "    g = round(10.17, 1)\n",
    "    h = round(10.12, 1)\n",
    "    return a, b, c, d, e, f, g, h\n",
    "\n",
    "a, b, c, d, e, f, g, h = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)\n",
    "print(\"e: {:.2f}\".format(e))\n",
    "print(\"f: {:.2f}\".format(f))\n",
    "print(\"g: {:.2f}\".format(g))\n",
    "print(\"h: {:.2f}\".format(h))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "353ecfc8",
   "metadata": {},
   "source": [
    "## max\n",
    "\n",
    "功能:返回给定参数的最大值。\n",
    "\n",
    "调用:`max(*data)`。\n",
    "\n",
    "入参: - `*data` - 若`*data`为单输入,则会比较单个输入内的各个元素,此时`data`必须为可迭代对象。若存在多个输入,则比较每个输入。`data`有效类型为`int`、`float`、`bool`、`list`、`tuple`、`dict`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "返回值:最大值。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b3eab550",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  3\n",
      "b:  3\n",
      "c:  3\n",
      "d:  4\n",
      "e:  c\n",
      "f:  (1, 4)\n",
      "g:  3\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = max([0, 1, 2, 3])\n",
    "    b = max((0, 1, 2, 3))\n",
    "    c = max({1: 10, 2: 20, 3: 3})\n",
    "    d = max(np.array([1, 2, 3, 4]))\n",
    "    e = max(('a', 'b', 'c'))\n",
    "    f = max((1, 2, 3), (1, 4))\n",
    "    g = max(ms.Tensor([1, 2, 3]))\n",
    "    return a, b, c, ms.Tensor(d), e, f, g\n",
    "\n",
    "a, b, c, d, e, f, g = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)\n",
    "print(\"e: \", e)\n",
    "print(\"f: \", f)\n",
    "print(\"g: \", g)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6637483d",
   "metadata": {},
   "source": [
    "## min\n",
    "\n",
    "功能:返回给定参数的最小值。\n",
    "\n",
    "调用:`min(*data)`。\n",
    "\n",
    "入参: - `*data` - 若`*data`为单输入,则会比较单个输入内的各个元素,此时`data`必须为可迭代对象。若存在多个输入,则比较每个输入。`data`有效类型为`int`、`float`、`bool`、`list`、`tuple`、`dict`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "返回值:最小值。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "fee90d88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  0\n",
      "b:  0\n",
      "c:  1\n",
      "d:  1\n",
      "e:  a\n",
      "f:  (1, 2, 3)\n",
      "g:  1\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = min([0, 1, 2, 3])\n",
    "    b = min((0, 1, 2, 3))\n",
    "    c = min({1: 10, 2: 20, 3: 3})\n",
    "    d = min(np.array([1, 2, 3, 4]))\n",
    "    e = min(('a', 'b', 'c'))\n",
    "    f = min((1, 2, 3), (1, 4))\n",
    "    g = min(ms.Tensor([1, 2, 3]))\n",
    "    return a, b, c, ms.Tensor(d), e, f, g\n",
    "\n",
    "a, b, c, d, e, f, g = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)\n",
    "print(\"e: \", e)\n",
    "print(\"f: \", f)\n",
    "print(\"g: \", g)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72ab9a6f",
   "metadata": {},
   "source": [
    "## sum\n",
    "\n",
    "功能:对输入序列进行求和计算。\n",
    "\n",
    "调用:`sum(x, n=0)`。\n",
    "\n",
    "入参:\n",
    "\n",
    "- `x` - 表示可迭代对象,有效类型为`list`、`tuple`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "- `n` - 表示指定相加的参数,缺省值为0。\n",
    "\n",
    "返回值:对`x`求和后与`n`相加得到的值。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c9da1f39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  3\n",
      "b:  13\n",
      "c:  6\n",
      "d:  16\n",
      "e:  [4 6]\n",
      "f:  [[ 4  6]\n",
      " [ 8 10]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = sum([0, 1, 2])\n",
    "    b = sum((0, 1, 2), 10)\n",
    "    c = sum(np.array([1, 2, 3]))\n",
    "    d = sum(ms.Tensor([1, 2, 3]), 10)\n",
    "    e = sum(ms.Tensor([[1, 2], [3, 4]]))\n",
    "    f = sum([1, ms.Tensor([[1, 2], [3, 4]]), ms.Tensor([[1, 2], [3, 4]])], ms.Tensor([[1, 1], [1, 1]]))\n",
    "    return a, b, ms.Tensor(c), d, e, f\n",
    "\n",
    "a, b, c, d, e, f = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)\n",
    "print(\"e: \", e)\n",
    "print(\"f: \", f)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db37cefe",
   "metadata": {},
   "source": [
    "## abs\n",
    "\n",
    "功能:返回给定参数的绝对值。\n",
    "\n",
    "调用:`abs(x)`。\n",
    "\n",
    "入参: - `x` - 有效类型为`int`、`float`、`bool`、`Tensor`以及第三方对象(例如`numpy.ndarray`)。\n",
    "\n",
    "返回值:绝对值。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "0f48f11e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  45\n",
      "b: 100.12\n",
      "c:  [1 2]\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "from mindspore import Tensor\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = abs(-45)\n",
    "    b = abs(100.12)\n",
    "    c = abs(Tensor([-1, 2]).asnumpy())\n",
    "    return a, b, c\n",
    "\n",
    "a, b, c = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: {:.2f}\".format(b))\n",
    "print(\"c: \", c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd2880eb",
   "metadata": {},
   "source": [
    "其中`abs(Tensor([-1, 2]).asnumpy())`属于高阶用法,更多介绍可见[AST扩展语法(LAX级别)](https://www.mindspore.cn/tutorials/zh-CN/master/compile/static_graph.html#ast%E6%89%A9%E5%B1%95%E8%AF%AD%E6%B3%95lax%E7%BA%A7%E5%88%AB)章节。\n",
    "\n",
    "## map\n",
    "\n",
    "功能:根据提供的函数对一个或者多个序列做映射,由映射的结果生成一个新的序列。当前要求多个序列中的元素个数一致。\n",
    "\n",
    "调用:`map(func, sequence, ...)`。\n",
    "\n",
    "入参:\n",
    "\n",
    "- `func` - 函数。\n",
    "\n",
    "- `sequence` - 一个或多个序列(`Tuple`或者`List`)。\n",
    "\n",
    "返回值:返回一个新的序列。\n",
    "\n",
    "示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "859aad65",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ret1:(5, 7, 9)\n",
      "ret2:[6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "\n",
    "def add(x, y):\n",
    "    return x + y\n",
    "\n",
    "@ms.jit()\n",
    "def test():\n",
    "    elements_a = (1, 2, 3)\n",
    "    elements_b = (4, 5, 6)\n",
    "    ret1 = map(add, elements_a, elements_b)\n",
    "    elements_c = [0, 1, 2]\n",
    "    elements_d = [6, 7, 8]\n",
    "    ret2 = map(add, elements_c, elements_d)\n",
    "    return ret1, ret2\n",
    "\n",
    "ret1, ret2 = test()\n",
    "print('ret1:{}'.format(ret1))\n",
    "print('ret2:{}'.format(ret2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "794e3ce3",
   "metadata": {},
   "source": [
    "## zip\n",
    "\n",
    "功能:将多个序列中对应位置的元素打包成一个个元组,然后由这些元组组成一个新序列,如果各个序列中的元素个数不一致,则生成的新序列与最短的那个长度相同。\n",
    "\n",
    "调用:`zip(sequence, ...)`。\n",
    "\n",
    "入参:`sequence` - 一个或多个序列(`Tuple`或`List`)。\n",
    "\n",
    "返回值:返回一个新的序列。\n",
    "\n",
    "示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "14ebda94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ret:((1, 4), (2, 5), (3, 6))\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "\n",
    "@ms.jit()\n",
    "def test():\n",
    "    elements_a = (1, 2, 3)\n",
    "    elements_b = (4, 5, 6, 7)\n",
    "    ret = zip(elements_a, elements_b)\n",
    "    return ret\n",
    "\n",
    "ret = test()\n",
    "print('ret:{}'.format(ret))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "decd8132",
   "metadata": {},
   "source": [
    "## range\n",
    "\n",
    "功能:根据起始值、结束值和步长创建一个`Tuple`。\n",
    "\n",
    "调用:\n",
    "\n",
    "- `range(start, stop, step)`\n",
    "\n",
    "- `range(start, stop)`\n",
    "\n",
    "- `range(stop)`\n",
    "\n",
    "入参:\n",
    "\n",
    "- `start` - 计数起始值,类型为`int`,默认为0。\n",
    "\n",
    "- `stop` - 计数结束值,但不包括在内,类型为`int`。\n",
    "\n",
    "- `step` - 步长,类型为`int`,默认为1。\n",
    "\n",
    "返回值:返回一个`Tuple`。\n",
    "\n",
    "示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a1fbd498",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x:(0, 2, 4)\n",
      "y:(0, 1, 2, 3, 4)\n",
      "z:(0, 1, 2)\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "\n",
    "@ms.jit()\n",
    "def test():\n",
    "    x = range(0, 6, 2)\n",
    "    y = range(0, 5)\n",
    "    z = range(3)\n",
    "    return x, y, z\n",
    "\n",
    "x, y, z = test()\n",
    "print('x:{}'.format(x))\n",
    "print('y:{}'.format(y))\n",
    "print('z:{}'.format(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c6a11b7",
   "metadata": {},
   "source": [
    "## enumerate\n",
    "\n",
    "功能:生成一个序列的索引序列,索引序列包含数据和对应下标。\n",
    "\n",
    "调用:\n",
    "\n",
    "- `enumerate(sequence, start=0)`\n",
    "\n",
    "- `enumerate(sequence)`\n",
    "\n",
    "入参:\n",
    "\n",
    "- `sequence` - 一个序列(`Tuple`、`List`、`Tensor`)。\n",
    "\n",
    "- `start` - 下标起始位置,类型为`int`,默认为0。\n",
    "\n",
    "返回值:返回一个`Tuple`。\n",
    "\n",
    "示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "5265694c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "m:((3, 100), (4, 200), (5, 300), (6, 400))\n",
      "n:((0, Tensor(shape=[2], dtype=Int64, value= [1, 2])), (1, Tensor(shape=[2], dtype=Int64, value= [3, 4])), (2, Tensor(shape=[2], dtype=Int64, value= [5, 6])))\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "import numpy as np\n",
    "\n",
    "y = ms.Tensor(np.array([[1, 2], [3, 4], [5, 6]]))\n",
    "\n",
    "@ms.jit()\n",
    "def test():\n",
    "    x = (100, 200, 300, 400)\n",
    "    m = enumerate(x, 3)\n",
    "    n = enumerate(y)\n",
    "    return m, n\n",
    "\n",
    "m, n = test()\n",
    "print('m:{}'.format(m))\n",
    "print('n:{}'.format(n))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90769137",
   "metadata": {},
   "source": [
    "## super\n",
    "\n",
    "功能:用于调用父类(超类)的一个方法,一般在`super`之后调用父类的方法。\n",
    "\n",
    "调用:\n",
    "\n",
    "- `super().xxx()`\n",
    "\n",
    "- `super(type, self).xxx()`\n",
    "\n",
    "入参:\n",
    "\n",
    "- `type` - 类。\n",
    "\n",
    "- `self` - 对象。\n",
    "\n",
    "返回值:返回父类的方法。\n",
    "\n",
    "示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "2755bcdb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "out: (9, 6)\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "from mindspore import nn, set_context\n",
    "\n",
    "set_context(mode=ms.GRAPH_MODE)\n",
    "\n",
    "class FatherNet(nn.Cell):\n",
    "    def __init__(self, x):\n",
    "        super(FatherNet, self).__init__(x)\n",
    "        self.x = x\n",
    "\n",
    "    def construct(self, x, y):\n",
    "        return self.x * x\n",
    "\n",
    "    def test_father(self, x):\n",
    "        return self.x + x\n",
    "\n",
    "class SingleSubNet(FatherNet):\n",
    "    def __init__(self, x, z):\n",
    "        super(SingleSubNet, self).__init__(x)\n",
    "        self.z = z\n",
    "\n",
    "    def construct(self, x, y):\n",
    "        ret_father_construct = super().construct(x, y)\n",
    "        ret_father_test = super(SingleSubNet, self).test_father(x)\n",
    "        return ret_father_construct, ret_father_test\n",
    "\n",
    "x = 3\n",
    "y = 6\n",
    "z = 9\n",
    "f_net = FatherNet(x)\n",
    "net = SingleSubNet(x, z)\n",
    "out = net(x, y)\n",
    "print(\"out:\", out)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ab9ad24",
   "metadata": {},
   "source": [
    "## pow\n",
    "\n",
    "功能:求幂。\n",
    "\n",
    "调用:`pow(x, y)`\n",
    "\n",
    "入参:\n",
    "\n",
    "- `x` - 底数, `Number`或`Tensor`。\n",
    "\n",
    "- `y` - 幂指数, `Number`或`Tensor`。\n",
    "\n",
    "返回值:返回`x`的`y`次幂,`Number`或`Tensor`。\n",
    "\n",
    "示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "3d67dadf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ret:[ 1  4 27]\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "import numpy as np\n",
    "\n",
    "x = ms.Tensor(np.array([1, 2, 3]))\n",
    "y = ms.Tensor(np.array([1, 2, 3]))\n",
    "\n",
    "@ms.jit()\n",
    "def test(x, y):\n",
    "    return pow(x, y)\n",
    "\n",
    "ret = test(x, y)\n",
    "\n",
    "print('ret:{}'.format(ret))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a254dbad",
   "metadata": {},
   "source": [
    "## print\n",
    "\n",
    "功能:用于打印。\n",
    "\n",
    "调用:`print(arg, ...)`\n",
    "\n",
    "入参:`arg` - 要打印的信息(`int` 、`float`、`bool`、`String`或`Tensor`,或者第三方库的数据类型)。\n",
    "\n",
    "返回值:无返回值。\n",
    "\n",
    "示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "9a834f00",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(shape=[3], dtype=Int32, value=[1 2 3])\n",
      "Tensor(shape=[], dtype=Int32, value=3)\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "import numpy as np\n",
    "\n",
    "x = ms.Tensor(np.array([1, 2, 3]), ms.int32)\n",
    "y = ms.Tensor(3, ms.int32)\n",
    "\n",
    "@ms.jit()\n",
    "def test(x, y):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    return x, y\n",
    "\n",
    "ret = test(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "386bddd5",
   "metadata": {},
   "source": [
    "## filter\n",
    "\n",
    "功能:根据提供的函数对一个序列的元素做判断,每个元素依次作为参数传入函数中,将返回结果不为0或False的元素组成新的序列。\n",
    "\n",
    "调用:`filter(func, sequence)`\n",
    "\n",
    "入参:\n",
    "\n",
    "- `func` - 函数。\n",
    "\n",
    "- `sequence` - 序列(`Tuple`或`List`)。\n",
    "\n",
    "返回值:返回一个新的序列。\n",
    "\n",
    "示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "6f5dbb25",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ret1:[1, 3, 5]\n",
      "ret2:[7, 9]\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "\n",
    "def is_odd(x):\n",
    "    if x % 2:\n",
    "        return True\n",
    "    return False\n",
    "\n",
    "@ms.jit()\n",
    "def test():\n",
    "    elements1 = (1, 2, 3, 4, 5)\n",
    "    ret1 = filter(is_odd, elements1)\n",
    "    elements2 = [6, 7, 8, 9, 10]\n",
    "    ret2 = filter(is_odd, elements2)\n",
    "    return ret1, ret2\n",
    "\n",
    "ret1, ret2 = test()\n",
    "print('ret1:{}'.format(ret1))\n",
    "print('ret2:{}'.format(ret2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "970d3f03",
   "metadata": {},
   "source": [
    "## type\n",
    "\n",
    "功能:输出入参的类型。\n",
    "\n",
    "有效输入:Number、list、tuple、dict、numpy.ndarray、常量Tensor。\n",
    "\n",
    "代码用例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "96795481",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  <class 'int'>\n",
      "b:  <class 'float'>\n",
      "c:  <class 'list'>\n",
      "d:  <class 'tuple'>\n",
      "e:  <class 'dict'>\n",
      "f:  <class 'numpy.ndarray'>\n",
      "g:  <class 'mindspore.common.tensor.Tensor'>\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import mindspore as ms\n",
    "\n",
    "@ms.jit\n",
    "def func():\n",
    "    a = type(1)\n",
    "    b = type(1.0)\n",
    "    c = type([1, 2, 3])\n",
    "    d = type((1, 2, 3))\n",
    "    e = type({'a': 1, 'b': 2})\n",
    "    f = type(np.array([1, 2, 3]))\n",
    "    g = type(ms.Tensor([1, 2, 3]))\n",
    "    return a, b, c, d, e, f, g\n",
    "\n",
    "a, b, c, d, e, f, g = func()\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"c: \", c)\n",
    "print(\"d: \", d)\n",
    "print(\"e: \", e)\n",
    "print(\"f: \", f)\n",
    "print(\"g: \", g)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b291b56d",
   "metadata": {},
   "source": [
    "> type作为Python的原生函数还有另外一种使用方法,即type(name, bases, dict)返回name类型的类对象,由于该用法应用场景较少,因此暂不支持。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "MindSpore",
   "language": "python",
   "name": "mindspore"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}