{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c8bb4682",
   "metadata": {},
   "source": [
    "# 方法与属性\n",
    "\n",
    "自定义类型中通常要定义一些方法和属性。\n",
    "\n",
    "## 手动添加属性\n",
    "\n",
    "属性可以手动添加:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "726dfdd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Leaf(object):\n",
    "    \"\"\"A leaf falling in the woods.\"\"\"\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8ada76bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "leaf = Leaf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "56c4eec8",
   "metadata": {},
   "outputs": [],
   "source": [
    "leaf.color = \"green\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cab8d1da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'green'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "leaf.color"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f535b118",
   "metadata": {},
   "source": [
    "这样添加的新属性只对当前定义的对象有效,不能在新对象上使用:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "70805a5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "leaf2 = Leaf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "879c9848",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Leaf' object has no attribute 'color'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[0;32mIn [6]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mleaf2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcolor\u001b[49m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'Leaf' object has no attribute 'color'"
     ]
    }
   ],
   "source": [
    "leaf2.color"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "fb1f35e6",
   "metadata": {},
   "source": [
    "## 添加普通方法\n",
    "\n",
    "添加普通方法需要用def关键字定义。向Leaf类中添加一个新方法,表示树叶下落:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2609b4fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Leaf(object):\n",
    "    \"\"\"A leaf falling in the woods.\"\"\"\n",
    "    def fall(self, season=\"autumn\"):\n",
    "        \"\"\"A leaf falls.\"\"\"\n",
    "        print(f\"A leaf falls in {season}!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4fdac1b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "leaf = Leaf()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69d1b644",
   "metadata": {},
   "source": [
    "查看帮助:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7912d38a",
   "metadata": {},
   "outputs": [],
   "source": [
    "leaf.fall?"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e3cae18f",
   "metadata": {},
   "source": [
    "调用该方法时,self参数不用传入,会自动转化为对象leaf,因此只需要传入season参数。不带参数时,season用默认值:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a3e3ed63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A leaf falls in autumn!\n"
     ]
    }
   ],
   "source": [
    "leaf.fall()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "08ad7bf9",
   "metadata": {},
   "source": [
    "## 构造方法`.__init__()`"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "22bffde0",
   "metadata": {},
   "source": [
    "通常通过构造方法`.__init__()`,在构造对象的时候添加属性。该方法在构造对象时会自动调用:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ff8278ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Leaf(object):\n",
    "    \"\"\"A leaf falling in the woods.\"\"\"\n",
    "    def __init__(self, color=\"green\"):\n",
    "        self.color = color"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "74fe950b",
   "metadata": {},
   "outputs": [],
   "source": [
    "leaf2 = Leaf(\"orange\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b2e29926",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'orange'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "leaf2.color"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8ed9f302",
   "metadata": {},
   "source": [
    "由于在`.__init__()`函数中指定了默认参数,在构造时,也可以不传入color参数时,`.color`属性为默认值“green”:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fc7ecd29",
   "metadata": {},
   "outputs": [],
   "source": [
    "leaf = Leaf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "cf68ac4a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'green'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "leaf.color"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad83c4c8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}