|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# 1. 类"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Python中的变量、列表、字典等其实都是对象。不涉及面向对象编程的理论部分,在本教程中对概念进行解释。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "类声明如下"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "```\n",
- "class class_name:\n",
- "\n",
- " Functions\n",
- "```"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [],
- "source": [
- "# 一个最简单的类\n",
- "class FirstClass:\n",
- " pass\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**pass** 在python中意味着什么都不做。 "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "上面声明了一个名为“FirstClass”的类对象,现在考虑一个具有“FirstClass”所有特征的“egclass”。所以你所要做的就是,将“egclass”等同于“FirstClass”。在python术语中,这称为创建实例。“egclass”是“FirstClass”的实例"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "egclass = FirstClass()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "__main__.FirstClass"
- ]
- },
- "execution_count": 4,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(egclass)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "type"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(FirstClass)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "现在让我们向类中添加一些“功能”。这样我们的FirstClass就有了更好的定义。类内的函数被称为该类的“方法”"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "大多数类都有一个名为`__init__`的函数。这些被称为魔术方法。在这个方法中,你基本上初始化了这个类的变量,或者任何适用于这个方法中指定的所有方法的初始化算法。类中的变量称为属性。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "这有助于简化实例的初始化过程。例如,\n",
- "\n",
- "在不使用魔法方法或被成为构造函数的`__init__`的情况下,我们必须定义一个**init()** 方法并调用**init()** 函数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "'FirstClass' object has no attribute 'init'",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m------------------------------------------\u001b[0m",
- "\u001b[0;31mAttributeError\u001b[0mTraceback (most recent call last)",
- "\u001b[0;32m<ipython-input-6-d15e7b8e3d78>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0meg0\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mFirstClass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0meg0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[0;31mAttributeError\u001b[0m: 'FirstClass' object has no attribute 'init'"
- ]
- }
- ],
- "source": [
- "eg0 = FirstClass()\n",
- "eg0.init()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "但是当构造函数被定义后,`__init__`被调用,这样初始化实例被创建。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "我们构造我们的`FirstClass`去接受两个变量名称和符号。\n",
- "\n",
- "我将会在稍后解释`self`。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [],
- "source": [
- "class FirstClass:\n",
- " \"\"\"My first class\"\"\"\n",
- " class_var = 10\n",
- " def __init__(self,name,symbol):\n",
- " self.name = name\n",
- " self.symbol = symbol"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "现在我们已经定义了一个函数而且添加了`__init__`方法。我们可以创建一个名为FirstClass的实例,该实例现在接受两个参数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [],
- "source": [
- "eg1 = FirstClass('one',1)\n",
- "eg2 = FirstClass('two',2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "one 1\n",
- "two 2\n",
- "My first class\n"
- ]
- }
- ],
- "source": [
- "print(eg1.name, eg1.symbol)\n",
- "print(eg2.name, eg2.symbol)\n",
- "print(eg1.__doc__)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**dir( )** 函数在查看类包含什么以及它提供了什么方法时非常方便。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {
- "scrolled": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['__class__',\n",
- " '__delattr__',\n",
- " '__dict__',\n",
- " '__dir__',\n",
- " '__doc__',\n",
- " '__eq__',\n",
- " '__format__',\n",
- " '__ge__',\n",
- " '__getattribute__',\n",
- " '__gt__',\n",
- " '__hash__',\n",
- " '__init__',\n",
- " '__init_subclass__',\n",
- " '__le__',\n",
- " '__lt__',\n",
- " '__module__',\n",
- " '__ne__',\n",
- " '__new__',\n",
- " '__reduce__',\n",
- " '__reduce_ex__',\n",
- " '__repr__',\n",
- " '__setattr__',\n",
- " '__sizeof__',\n",
- " '__str__',\n",
- " '__subclasshook__',\n",
- " '__weakref__',\n",
- " 'class_var']"
- ]
- },
- "execution_count": 13,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(FirstClass)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'My first class'"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "FirstClass.__doc__"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "实例的**dir()** 也显示了它定义的属性。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['__class__',\n",
- " '__delattr__',\n",
- " '__dict__',\n",
- " '__dir__',\n",
- " '__doc__',\n",
- " '__eq__',\n",
- " '__format__',\n",
- " '__ge__',\n",
- " '__getattribute__',\n",
- " '__gt__',\n",
- " '__hash__',\n",
- " '__init__',\n",
- " '__init_subclass__',\n",
- " '__le__',\n",
- " '__lt__',\n",
- " '__module__',\n",
- " '__ne__',\n",
- " '__new__',\n",
- " '__reduce__',\n",
- " '__reduce_ex__',\n",
- " '__repr__',\n",
- " '__setattr__',\n",
- " '__sizeof__',\n",
- " '__str__',\n",
- " '__subclasshook__',\n",
- " '__weakref__',\n",
- " 'class_var',\n",
- " 'name',\n",
- " 'symbol']"
- ]
- },
- "execution_count": 15,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(eg1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "稍微改变一下FirstClass函数,"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [],
- "source": [
- "class FirstClass:\n",
- " def __init__(self,name,symbol):\n",
- " self.n = name\n",
- " self.s = symbol"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "将self.name和self.symbol转化成self.n和self.s会得到:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [],
- "source": [
- "eg1 = FirstClass('one',1)\n",
- "eg2 = FirstClass('two',2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "'FirstClass' object has no attribute 'name'",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m------------------------------------------\u001b[0m",
- "\u001b[0;31mAttributeError\u001b[0mTraceback (most recent call last)",
- "\u001b[0;32m<ipython-input-18-4ab7dec1c737>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;31mAttributeError\u001b[0m: 'FirstClass' object has no attribute 'name'"
- ]
- }
- ],
- "source": [
- "print(eg1.name, eg1.symbol)\n",
- "print(eg2.name, eg2.symbol)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "AttributeError, 还记得变量就是类中的属性吗?因此,这意味着我们没有为实例提供正确的属性。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['__class__',\n",
- " '__delattr__',\n",
- " '__dict__',\n",
- " '__dir__',\n",
- " '__doc__',\n",
- " '__eq__',\n",
- " '__format__',\n",
- " '__ge__',\n",
- " '__getattribute__',\n",
- " '__gt__',\n",
- " '__hash__',\n",
- " '__init__',\n",
- " '__init_subclass__',\n",
- " '__le__',\n",
- " '__lt__',\n",
- " '__module__',\n",
- " '__ne__',\n",
- " '__new__',\n",
- " '__reduce__',\n",
- " '__reduce_ex__',\n",
- " '__repr__',\n",
- " '__setattr__',\n",
- " '__sizeof__',\n",
- " '__str__',\n",
- " '__subclasshook__',\n",
- " '__weakref__',\n",
- " 'n',\n",
- " 's']"
- ]
- },
- "execution_count": 19,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(eg1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "one 1\n",
- "two 2\n"
- ]
- }
- ],
- "source": [
- "print(eg1.n, eg1.s)\n",
- "print(eg2.n, eg2.s)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "现在我们解决了这个错误。现在让我们比较一下我们看到的两个例子。\n",
- "\n",
- "当我声明self.name和self.symbol时,使用eg1.name和eg1.symbol没有属性错误。当我声明self.n和self.s时,使用eg1.n和eg1.s没有属性错误。\n",
- "\n",
- "从以上我们可以得出self就是实例本身。\n",
- "\n",
- "记住,self不是预定义的,它是用户定义的。你可以利用任何你觉得舒服的东西。但是使用self已经成为一种常见的做法。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [],
- "source": [
- "class FirstClass:\n",
- " def __init__(asdf1234,name,symbol):\n",
- " asdf1234.n = name\n",
- " asdf1234.s = symbol"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [],
- "source": [
- "eg1 = FirstClass('one',1)\n",
- "eg2 = FirstClass('two',2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "one 1\n",
- "two 2\n"
- ]
- }
- ],
- "source": [
- "print(eg1.n, eg1.s)\n",
- "print(eg2.n, eg2.s)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "因为eg1和eg2是FirstClass的实例,所以它不需要被限制在FirstClass本身。它可以通过声明其他属性来扩展自己,而不需要在FirstClass中声明属性。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [],
- "source": [
- "eg1.cube = 1\n",
- "eg2.cube = 8"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['__class__',\n",
- " '__delattr__',\n",
- " '__dict__',\n",
- " '__dir__',\n",
- " '__doc__',\n",
- " '__eq__',\n",
- " '__format__',\n",
- " '__ge__',\n",
- " '__getattribute__',\n",
- " '__gt__',\n",
- " '__hash__',\n",
- " '__init__',\n",
- " '__init_subclass__',\n",
- " '__le__',\n",
- " '__lt__',\n",
- " '__module__',\n",
- " '__ne__',\n",
- " '__new__',\n",
- " '__reduce__',\n",
- " '__reduce_ex__',\n",
- " '__repr__',\n",
- " '__setattr__',\n",
- " '__sizeof__',\n",
- " '__str__',\n",
- " '__subclasshook__',\n",
- " '__weakref__',\n",
- " 'cube',\n",
- " 'n',\n",
- " 's']"
- ]
- },
- "execution_count": 24,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(eg1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "collapsed": true
- },
- "source": [
- "就像我们前面看到的全局变量和局部变量一样,即使类也有自己的变量类型。\n",
- "\n",
- "**类属性**:在方法外部定义的属性,适用于所有实例。\n",
- "\n",
- "**实例属性**:在方法内部定义的属性,只适用于该方法,并且对每个实例都是唯一的。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {},
- "outputs": [],
- "source": [
- "class FirstClass:\n",
- " test = 'test'\n",
- " def __init__(self,name,symbol):\n",
- " self.name = name\n",
- " self.symbol = symbol"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "这里test是一个类属性,而name是一个实例属性。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "test4\n"
- ]
- }
- ],
- "source": [
- "eg3 = FirstClass('Three',3)\n",
- "eg4 = FirstClass('Four', 4)\n",
- "eg4.test = 'test4'\n",
- "print(eg4.test)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "test Three\n"
- ]
- }
- ],
- "source": [
- "print(eg3.test, eg3.name)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "让我们添加更多的方法到FirstClass。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {},
- "outputs": [],
- "source": [
- "class FirstClass:\n",
- " def __init__(self,name,symbol):\n",
- " self.name = name\n",
- " self.symbol = symbol\n",
- " def square(self):\n",
- " return self.symbol * self.symbol\n",
- " def cube(self):\n",
- " return self.symbol * self.symbol * self.symbol\n",
- " def multiply(self, x):\n",
- " return self.symbol * x"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [],
- "source": [
- "eg4 = FirstClass('Five',5)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "25\n",
- "125\n"
- ]
- }
- ],
- "source": [
- "print(eg4.square())\n",
- "print(eg4.cube())"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "10"
- ]
- },
- "execution_count": 33,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "eg4.multiply(2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "以上也可以写成:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "10"
- ]
- },
- "execution_count": 34,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "FirstClass.multiply(eg4,2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 2. 继承"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在某些情况下,新类需要具有已定义类的所有特征。因此,新类可以“继承”前一个类,并向其添加自己的方法,这称为继承。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "考虑类Person类具有薪水的方法。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {},
- "outputs": [],
- "source": [
- "class Person:\n",
- " def __init__(self,name,age):\n",
- " self.name = name\n",
- " self.age = age\n",
- " def salary(self, value):\n",
- " self.money = value\n",
- " print(self.name,\"earns\",self.money)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 37,
- "metadata": {},
- "outputs": [],
- "source": [
- "a = Person('Kartik',26)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Kartik earns 40000\n"
- ]
- }
- ],
- "source": [
- "a.salary(40000)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 39,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['__class__',\n",
- " '__delattr__',\n",
- " '__dict__',\n",
- " '__dir__',\n",
- " '__doc__',\n",
- " '__eq__',\n",
- " '__format__',\n",
- " '__ge__',\n",
- " '__getattribute__',\n",
- " '__gt__',\n",
- " '__hash__',\n",
- " '__init__',\n",
- " '__init_subclass__',\n",
- " '__le__',\n",
- " '__lt__',\n",
- " '__module__',\n",
- " '__ne__',\n",
- " '__new__',\n",
- " '__reduce__',\n",
- " '__reduce_ex__',\n",
- " '__repr__',\n",
- " '__setattr__',\n",
- " '__sizeof__',\n",
- " '__str__',\n",
- " '__subclasshook__',\n",
- " '__weakref__',\n",
- " 'salary']"
- ]
- },
- "execution_count": 39,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(Person)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "现在考虑另一个Artist类,告诉我们艺术家挣的钱的数量和他的艺术形式。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 40,
- "metadata": {},
- "outputs": [],
- "source": [
- "class Artist:\n",
- " def __init__(self,name,age):\n",
- " self.name = name\n",
- " self.age = age\n",
- " def salary(self,value):\n",
- " self.money = value\n",
- " print(self.name,\"earns\",self.money)\n",
- " def artform(self, job):\n",
- " self.job = job\n",
- " print(self.name,\"is a\", self.job)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 41,
- "metadata": {},
- "outputs": [],
- "source": [
- "b = Artist('Nitin',20)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Nitin earns 50000\n",
- "Nitin is a Musician\n"
- ]
- }
- ],
- "source": [
- "b.salary(50000)\n",
- "b.artform('Musician')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 43,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['__class__',\n",
- " '__delattr__',\n",
- " '__dict__',\n",
- " '__dir__',\n",
- " '__doc__',\n",
- " '__eq__',\n",
- " '__format__',\n",
- " '__ge__',\n",
- " '__getattribute__',\n",
- " '__gt__',\n",
- " '__hash__',\n",
- " '__init__',\n",
- " '__init_subclass__',\n",
- " '__le__',\n",
- " '__lt__',\n",
- " '__module__',\n",
- " '__ne__',\n",
- " '__new__',\n",
- " '__reduce__',\n",
- " '__reduce_ex__',\n",
- " '__repr__',\n",
- " '__setattr__',\n",
- " '__sizeof__',\n",
- " '__str__',\n",
- " '__subclasshook__',\n",
- " '__weakref__',\n",
- " 'artform',\n",
- " 'salary']"
- ]
- },
- "execution_count": 43,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(Artist)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "money 方法和salary 方法是一样的。因此,我们可以将该方法推广到工资类,并将软件工程师类继承到美术师类。现在艺术类变成了,"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 44,
- "metadata": {},
- "outputs": [],
- "source": [
- "class Artist(Person):\n",
- " def artform(self, job):\n",
- " self.job = job\n",
- " print(self.name,\"is a\", self.job)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 46,
- "metadata": {},
- "outputs": [],
- "source": [
- "c = Artist('Nishanth',21)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 47,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['__class__',\n",
- " '__delattr__',\n",
- " '__dict__',\n",
- " '__dir__',\n",
- " '__doc__',\n",
- " '__eq__',\n",
- " '__format__',\n",
- " '__ge__',\n",
- " '__getattribute__',\n",
- " '__gt__',\n",
- " '__hash__',\n",
- " '__init__',\n",
- " '__init_subclass__',\n",
- " '__le__',\n",
- " '__lt__',\n",
- " '__module__',\n",
- " '__ne__',\n",
- " '__new__',\n",
- " '__reduce__',\n",
- " '__reduce_ex__',\n",
- " '__repr__',\n",
- " '__setattr__',\n",
- " '__sizeof__',\n",
- " '__str__',\n",
- " '__subclasshook__',\n",
- " '__weakref__',\n",
- " 'artform',\n",
- " 'salary']"
- ]
- },
- "execution_count": 47,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(Artist)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Nishanth earns 60000\n",
- "Nishanth is a Dancer\n"
- ]
- }
- ],
- "source": [
- "c.salary(60000)\n",
- "c.artform('Dancer')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "假设在继承一个特定方法的时候,该方法不适合新类。可以通过在新类中用相同的名称再次定义该方法来重写该方法。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 50,
- "metadata": {},
- "outputs": [],
- "source": [
- "class Artist(Person):\n",
- " def artform(self, job):\n",
- " self.job = job\n",
- " print(self.name,\"is a\", self.job)\n",
- " def salary(self, value):\n",
- " self.money = value\n",
- " print(self.name,\"earns\",self.money)\n",
- " print(\"I am overriding the SoftwareEngineer class's salary method\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 51,
- "metadata": {},
- "outputs": [],
- "source": [
- "c = Artist('Nishanth',21)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 52,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Nishanth earns 60000\n",
- "I am overriding the SoftwareEngineer class's salary method\n",
- "Nishanth is a Dancer\n"
- ]
- }
- ],
- "source": [
- "c.salary(60000)\n",
- "c.artform('Dancer')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果不确定方法将被调用多少次,那么就很难声明那么多变量来携带每个结果,因此最好声明一个列表并附加结果。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 53,
- "metadata": {},
- "outputs": [],
- "source": [
- "class EmptyList:\n",
- " def __init__(self):\n",
- " self.data = []\n",
- " def one(self,x):\n",
- " self.data.append(x)\n",
- " def two(self, x ):\n",
- " self.data.append(x**2)\n",
- " def three(self, x):\n",
- " self.data.append(x**3)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 54,
- "metadata": {},
- "outputs": [],
- "source": [
- "xc = EmptyList()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 55,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1]\n"
- ]
- }
- ],
- "source": [
- "xc.one(1)\n",
- "print(xc.data)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "因为xc.data是一个列表,直接的列表操作也是可以进行的。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 56,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 8]\n"
- ]
- }
- ],
- "source": [
- "xc.data.append(8)\n",
- "print(xc.data)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 57,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 8, 9]\n"
- ]
- }
- ],
- "source": [
- "xc.two(3)\n",
- "print(xc.data)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果输入参数的数量因实例而异,则可以使用星号。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 58,
- "metadata": {},
- "outputs": [],
- "source": [
- "class NotSure:\n",
- " def __init__(self, *args):\n",
- " self.data = ''.join(list(args)) "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 63,
- "metadata": {},
- "outputs": [],
- "source": [
- "yz = NotSure('I', 'Do' , 'Not', 'Know', 'What', 'To','Type')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 64,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'IDoNotKnowWhatToType'"
- ]
- },
- "execution_count": 64,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "yz.data"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# 3. 接下来应该怎么做"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "找各个方面的练习题,并独立完成能帮助你掌握Python的窍门,例如给自己一个问题并解决它们,你还可以在任何编程竞赛平台上提交问题求解。你编写的代码越多,你发现的越多,你就越开始欣赏这门语言。强烈建议把[Python作业](https://gitee.com/pi-lab/machinelearning_homework/blob/master/homework_01_python/README.md)完成,并在[其他编程练习](https://gitee.com/pi-lab/machinelearning_homework/blob/master/homework_01_python/README.md#references)里面找一些练习题或者项目做一下。\n",
- "\n",
- "现在已经向你介绍了Python,您可以尝试您感兴趣的领域中的不同Python库。我强烈建议您查看这个Python框架、库和软件列表 http://awesome-python.com\n",
- "\n",
- "\n",
- "Pyton 教程:\n",
- "* [Python tutorial (廖雪峰)](https://www.liaoxuefeng.com/wiki/1016959663602400)\n",
- "* [Python基础教程](https://www.runoob.com/python/python-tutorial.html)\n",
- "* [Python官方教程(中文版)](https://docs.python.org/zh-cn/3/tutorial/index.html)\n",
- "* Python官方文档: https://docs.python.org/3/\n",
- "* 本教程来源于:https://github.com/rajathkumarmp/Python-Lectures \n",
- "\n",
- "\n",
- "**最后,享受解决问题的快乐!因为生命短暂,你需要Python!**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "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.6.9"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|