|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# 1. 类"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Python中的变量、列表、字典等其实都是对象。不涉及面向对象编程的理论部分,在本教程中对概念进行解释。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "类声明如下"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "class class_name:\n",
- "\n",
- " Functions"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "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": 2,
- "metadata": {},
- "outputs": [],
- "source": [
- "egclass = FirstClass()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "__main__.FirstClass"
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(egclass)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "type"
- ]
- },
- "execution_count": 4,
- "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": 8,
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "'FirstClass' object has no attribute 'init'",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m<ipython-input-8-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[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[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": 13,
- "metadata": {},
- "outputs": [],
- "source": [
- "class FirstClass:\n",
- " \"\"\"My first class\"\"\"\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": 18,
- "metadata": {},
- "outputs": [],
- "source": [
- "eg1 = FirstClass('one',1)\n",
- "eg2 = FirstClass('two',2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "onex 11\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": 16,
- "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",
- " '__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__']"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(FirstClass)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'My first class'"
- ]
- },
- "execution_count": 24,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "FirstClass.__doc__"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "实例的**dir()** 也显示了它定义的属性。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "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",
- " '__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",
- " 'name',\n",
- " 'symbol']"
- ]
- },
- "execution_count": 25,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(eg1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "稍微改变一下FirstClass函数,"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "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": 22,
- "metadata": {},
- "outputs": [],
- "source": [
- "eg1 = FirstClass('one',1)\n",
- "eg2 = FirstClass('two',2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "'FirstClass' object has no attribute 'name'",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m<ipython-input-23-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[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[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": 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",
- " '__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": 24,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(eg1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "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": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "class FirstClass:\n",
- " def __init__(asdf1234,name,symbol):\n",
- " asdf1234.n = name\n",
- " asdf1234.s = symbol"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "eg1 = FirstClass('one',1)\n",
- "eg2 = FirstClass('two',2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "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": 25,
- "metadata": {},
- "outputs": [],
- "source": [
- "eg1.cube = 1\n",
- "eg2.cube = 8"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "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",
- " '__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": 26,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(eg1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "collapsed": true
- },
- "source": [
- "就像我们前面看到的全局变量和局部变量一样,即使类也有自己的变量类型。\n",
- "\n",
- "**类属性**:在方法外部定义的属性,适用于所有实例。\n",
- "\n",
- "**实例属性**:在方法内部定义的属性,只适用于该方法,并且对每个实例都是唯一的。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "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": 28,
- "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": 30,
- "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": 34,
- "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": 35,
- "metadata": {},
- "outputs": [],
- "source": [
- "eg4 = FirstClass('Five',5)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 37,
- "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": 27,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "10"
- ]
- },
- "execution_count": 27,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "eg4.multiply(2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "以上也可以写成:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "type object 'FirstClass' has no attribute 'multiply'",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m<ipython-input-5-448ee6ad2a26>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mFirstClass\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmultiply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\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: type object 'FirstClass' has no attribute 'multiply'"
- ]
- }
- ],
- "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": 2,
- "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": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "a = Person('Kartik',26)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Kartik earns 40000\n"
- ]
- }
- ],
- "source": [
- "a.salary(40000)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "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": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(Person)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "现在考虑另一个Artist类,告诉我们艺术家挣的钱的数量和他的艺术形式。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "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": 43,
- "metadata": {},
- "outputs": [],
- "source": [
- "b = Artist('Nitin',20)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 44,
- "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": 45,
- "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",
- " '__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",
- " 'money']"
- ]
- },
- "execution_count": 45,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(Artist)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "money 方法和salary 方法是一样的。因此,我们可以将该方法推广到工资类,并将软件工程师类继承到美术师类。现在艺术类变成了,"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "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": 7,
- "metadata": {},
- "outputs": [],
- "source": [
- "c = Artist('Nishanth',21)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "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",
- " '__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": 48,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dir(Artist)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "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": 9,
- "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": 10,
- "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": 11,
- "metadata": {},
- "outputs": [],
- "source": [
- "xc = EmptyList()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "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": 60,
- "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": 61,
- "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": 62,
- "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的窍门。给自己一个问题陈述并解决它们。您还可以在任何竞争的编码平台上提交问题求解。你编写的代码越多,你发现的越多,你就越开始欣赏这门语言。\n",
- "\n",
- "现在已经向您介绍了python,您可以尝试您感兴趣的领域中的不同python库。我强烈建议您查看这个Python框架、库和软件列表http://awesome-python.com\n",
- "\n",
- "\n",
- "python官方文档: https://docs.python.org/3/\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",
- "\n",
- "你也可以参考一些Python的练习程序,是由 Kartik Kannapur所写的。 Github Repo : 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
- }
|