{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4ab6270b",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Vector:\n",
    "    def __init__(self, lst):\n",
    "        self._values = lst\n",
    "\n",
    "    def __getitem__(self, index):\n",
    "        \"\"\"取向量的第index个元素\"\"\"\n",
    "        return self._values[index]\n",
    "\n",
    "    def __len__(self):\n",
    "        \"\"\"返回向量长度（有多少个元素）\"\"\"\n",
    "        return len(self._values)\n",
    "    # 显示属性\n",
    "    def __repr__(self):  \n",
    "        print('repr')\n",
    "        return \"Vector({})\".format(self._values)\n",
    "\n",
    "    def __str__(self):\n",
    "        return \"({})\".format(\", \".join(str(e) for e in self._values))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4783a489",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 2)\n",
      "len(vec) = 2\n",
      "vec[0] = 5, vec[1] = 2\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    vec = Vector([5, 2])\n",
    "    print(vec)\n",
    "    print(\"len(vec) = {}\".format(len(vec)))\n",
    "    print(\"vec[0] = {}, vec[1] = {}\".format(vec[0], vec[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ddcc861",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97684f6d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "924a01b7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:py_36] *",
   "language": "python",
   "name": "conda-env-py_36-py"
  },
  "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.6.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
