You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

7_Class.ipynb 28 kB

6 years ago
3 years ago
6 years ago
2 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
3 years ago
6 years ago
3 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# 类"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "Python中的变量、列表、字典等其实都是类,因为Python从设计之初就已经是一门面向对象的语言。\n",
  15. "\n",
  16. "本节常见的定义和概念定义:\n",
  17. "* 类(Class): 用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。\n",
  18. "* 对象:通过类定义的数据结构实例。对象包括两个数据成员(类变量和实例变量)和方法。例如我们定义了一个 `Person` 类,而具体的人,比如小明,小黄就是 `Person` 类的实例\n",
  19. "* 属性: 描述该类具有的特征,比如人类具备的属性,身份证,姓名,性别,身高,体重等等都是属性\n",
  20. "* 方法:是该类对象的行为,例如这个男孩会打篮球,那个女孩会唱歌等等都是属于方法,常常通过方法改变一些类中的属性值"
  21. ]
  22. },
  23. {
  24. "cell_type": "markdown",
  25. "metadata": {},
  26. "source": [
  27. "类声明如下:"
  28. ]
  29. },
  30. {
  31. "cell_type": "markdown",
  32. "metadata": {},
  33. "source": [
  34. "```\n",
  35. "class class_name:\n",
  36. "\n",
  37. " Functions\n",
  38. "```"
  39. ]
  40. },
  41. {
  42. "cell_type": "code",
  43. "execution_count": 3,
  44. "metadata": {},
  45. "outputs": [],
  46. "source": [
  47. "# 一个最简单的类\n",
  48. "class FirstClass:\n",
  49. " pass\n"
  50. ]
  51. },
  52. {
  53. "cell_type": "markdown",
  54. "metadata": {},
  55. "source": [
  56. "**pass** 在Python中意味着什么都不做。 "
  57. ]
  58. },
  59. {
  60. "cell_type": "markdown",
  61. "metadata": {},
  62. "source": [
  63. "上面声明了一个名为“FirstClass”的类对象,现在考虑一个具有“FirstClass”所有特征的“egclass”。所以你所要做的就是,将“egclass”等同于“FirstClass”。在python术语中,这称为创建实例。“egclass”是“FirstClass”的实例"
  64. ]
  65. },
  66. {
  67. "cell_type": "code",
  68. "execution_count": 4,
  69. "metadata": {},
  70. "outputs": [],
  71. "source": [
  72. "egclass = FirstClass()"
  73. ]
  74. },
  75. {
  76. "cell_type": "code",
  77. "execution_count": 5,
  78. "metadata": {},
  79. "outputs": [
  80. {
  81. "data": {
  82. "text/plain": [
  83. "__main__.FirstClass"
  84. ]
  85. },
  86. "execution_count": 5,
  87. "metadata": {},
  88. "output_type": "execute_result"
  89. }
  90. ],
  91. "source": [
  92. "type(egclass)"
  93. ]
  94. },
  95. {
  96. "cell_type": "code",
  97. "execution_count": 6,
  98. "metadata": {},
  99. "outputs": [
  100. {
  101. "data": {
  102. "text/plain": [
  103. "type"
  104. ]
  105. },
  106. "execution_count": 6,
  107. "metadata": {},
  108. "output_type": "execute_result"
  109. }
  110. ],
  111. "source": [
  112. "type(FirstClass)"
  113. ]
  114. },
  115. {
  116. "cell_type": "markdown",
  117. "metadata": {},
  118. "source": [
  119. "现在让我们向类中添加一些“功能”。这样我们的FirstClass就有了更好的定义。类内的函数被称为该类的“方法”"
  120. ]
  121. },
  122. {
  123. "cell_type": "markdown",
  124. "metadata": {},
  125. "source": [
  126. "大多数类都有一个名为`__init__`的函数。这些被称为魔术方法。在这个方法中,你基本上初始化了这个类的变量,或者任何适用于这个方法中指定的所有方法的初始化算法。类中的变量称为属性。"
  127. ]
  128. },
  129. {
  130. "cell_type": "markdown",
  131. "metadata": {},
  132. "source": [
  133. "当构造函数被定义后,`__init__`被调用,这样初始化实例被创建。"
  134. ]
  135. },
  136. {
  137. "cell_type": "markdown",
  138. "metadata": {},
  139. "source": [
  140. "我们构造我们的`FirstClass`去接受两个变量名称和符号。\n",
  141. "\n",
  142. "我将会在稍后解释`self`。"
  143. ]
  144. },
  145. {
  146. "cell_type": "code",
  147. "execution_count": 7,
  148. "metadata": {},
  149. "outputs": [],
  150. "source": [
  151. "class FirstClass:\n",
  152. " \"\"\"My first class\"\"\"\n",
  153. " class_var = 10\n",
  154. " def __init__(self,name,value):\n",
  155. " self.name = name\n",
  156. " self.value = value"
  157. ]
  158. },
  159. {
  160. "cell_type": "markdown",
  161. "metadata": {},
  162. "source": [
  163. "现在我们已经定义了一个函数而且添加了`__init__`方法。我们可以创建一个名为FirstClass的实例,该实例现在接受两个参数。"
  164. ]
  165. },
  166. {
  167. "cell_type": "code",
  168. "execution_count": 8,
  169. "metadata": {},
  170. "outputs": [],
  171. "source": [
  172. "eg1 = FirstClass('one',1)\n",
  173. "eg2 = FirstClass('two',2)"
  174. ]
  175. },
  176. {
  177. "cell_type": "code",
  178. "execution_count": 9,
  179. "metadata": {},
  180. "outputs": [
  181. {
  182. "name": "stdout",
  183. "output_type": "stream",
  184. "text": [
  185. "one 1\n",
  186. "two 2\n",
  187. "My first class\n"
  188. ]
  189. }
  190. ],
  191. "source": [
  192. "print(eg1.name, eg1.value)\n",
  193. "print(eg2.name, eg2.value)\n",
  194. "print(eg1.__doc__)"
  195. ]
  196. },
  197. {
  198. "cell_type": "markdown",
  199. "metadata": {},
  200. "source": [
  201. "**dir( )** 函数在查看类包含什么以及它提供了什么方法时非常方便。"
  202. ]
  203. },
  204. {
  205. "cell_type": "code",
  206. "execution_count": 10,
  207. "metadata": {
  208. "scrolled": false
  209. },
  210. "outputs": [
  211. {
  212. "data": {
  213. "text/plain": [
  214. "['__class__',\n",
  215. " '__delattr__',\n",
  216. " '__dict__',\n",
  217. " '__dir__',\n",
  218. " '__doc__',\n",
  219. " '__eq__',\n",
  220. " '__format__',\n",
  221. " '__ge__',\n",
  222. " '__getattribute__',\n",
  223. " '__gt__',\n",
  224. " '__hash__',\n",
  225. " '__init__',\n",
  226. " '__init_subclass__',\n",
  227. " '__le__',\n",
  228. " '__lt__',\n",
  229. " '__module__',\n",
  230. " '__ne__',\n",
  231. " '__new__',\n",
  232. " '__reduce__',\n",
  233. " '__reduce_ex__',\n",
  234. " '__repr__',\n",
  235. " '__setattr__',\n",
  236. " '__sizeof__',\n",
  237. " '__str__',\n",
  238. " '__subclasshook__',\n",
  239. " '__weakref__',\n",
  240. " 'class_var']"
  241. ]
  242. },
  243. "execution_count": 10,
  244. "metadata": {},
  245. "output_type": "execute_result"
  246. }
  247. ],
  248. "source": [
  249. "dir(FirstClass)"
  250. ]
  251. },
  252. {
  253. "cell_type": "code",
  254. "execution_count": 11,
  255. "metadata": {},
  256. "outputs": [
  257. {
  258. "data": {
  259. "text/plain": [
  260. "'My first class'"
  261. ]
  262. },
  263. "execution_count": 11,
  264. "metadata": {},
  265. "output_type": "execute_result"
  266. }
  267. ],
  268. "source": [
  269. "FirstClass.__doc__"
  270. ]
  271. },
  272. {
  273. "cell_type": "markdown",
  274. "metadata": {},
  275. "source": [
  276. "实例的**dir()** 也显示了它定义的属性。"
  277. ]
  278. },
  279. {
  280. "cell_type": "code",
  281. "execution_count": 12,
  282. "metadata": {},
  283. "outputs": [
  284. {
  285. "data": {
  286. "text/plain": [
  287. "['__class__',\n",
  288. " '__delattr__',\n",
  289. " '__dict__',\n",
  290. " '__dir__',\n",
  291. " '__doc__',\n",
  292. " '__eq__',\n",
  293. " '__format__',\n",
  294. " '__ge__',\n",
  295. " '__getattribute__',\n",
  296. " '__gt__',\n",
  297. " '__hash__',\n",
  298. " '__init__',\n",
  299. " '__init_subclass__',\n",
  300. " '__le__',\n",
  301. " '__lt__',\n",
  302. " '__module__',\n",
  303. " '__ne__',\n",
  304. " '__new__',\n",
  305. " '__reduce__',\n",
  306. " '__reduce_ex__',\n",
  307. " '__repr__',\n",
  308. " '__setattr__',\n",
  309. " '__sizeof__',\n",
  310. " '__str__',\n",
  311. " '__subclasshook__',\n",
  312. " '__weakref__',\n",
  313. " 'class_var',\n",
  314. " 'name',\n",
  315. " 'value']"
  316. ]
  317. },
  318. "execution_count": 12,
  319. "metadata": {},
  320. "output_type": "execute_result"
  321. }
  322. ],
  323. "source": [
  324. "dir(eg1)"
  325. ]
  326. },
  327. {
  328. "cell_type": "markdown",
  329. "metadata": {},
  330. "source": [
  331. "稍微改变一下FirstClass函数,"
  332. ]
  333. },
  334. {
  335. "cell_type": "code",
  336. "execution_count": 13,
  337. "metadata": {},
  338. "outputs": [],
  339. "source": [
  340. "class FirstClass:\n",
  341. " def __init__(self,name,value):\n",
  342. " self.n = name\n",
  343. " self.v = value"
  344. ]
  345. },
  346. {
  347. "cell_type": "markdown",
  348. "metadata": {},
  349. "source": [
  350. "将self.name和self.symbol转化成self.n和self.s会得到:"
  351. ]
  352. },
  353. {
  354. "cell_type": "code",
  355. "execution_count": 14,
  356. "metadata": {},
  357. "outputs": [],
  358. "source": [
  359. "eg1 = FirstClass('one',1)\n",
  360. "eg2 = FirstClass('two',2)"
  361. ]
  362. },
  363. {
  364. "cell_type": "code",
  365. "execution_count": 15,
  366. "metadata": {},
  367. "outputs": [
  368. {
  369. "ename": "AttributeError",
  370. "evalue": "'FirstClass' object has no attribute 'name'",
  371. "output_type": "error",
  372. "traceback": [
  373. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  374. "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
  375. "\u001b[0;32m<ipython-input-15-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",
  376. "\u001b[0;31mAttributeError\u001b[0m: 'FirstClass' object has no attribute 'name'"
  377. ]
  378. }
  379. ],
  380. "source": [
  381. "print(eg1.name, eg1.symbol)\n",
  382. "print(eg2.name, eg2.symbol)"
  383. ]
  384. },
  385. {
  386. "cell_type": "markdown",
  387. "metadata": {},
  388. "source": [
  389. "AttributeError, 还记得变量就是类中的属性吗?因此,这意味着我们没有为实例提供正确的属性。"
  390. ]
  391. },
  392. {
  393. "cell_type": "code",
  394. "execution_count": 16,
  395. "metadata": {},
  396. "outputs": [
  397. {
  398. "data": {
  399. "text/plain": [
  400. "['__class__',\n",
  401. " '__delattr__',\n",
  402. " '__dict__',\n",
  403. " '__dir__',\n",
  404. " '__doc__',\n",
  405. " '__eq__',\n",
  406. " '__format__',\n",
  407. " '__ge__',\n",
  408. " '__getattribute__',\n",
  409. " '__gt__',\n",
  410. " '__hash__',\n",
  411. " '__init__',\n",
  412. " '__init_subclass__',\n",
  413. " '__le__',\n",
  414. " '__lt__',\n",
  415. " '__module__',\n",
  416. " '__ne__',\n",
  417. " '__new__',\n",
  418. " '__reduce__',\n",
  419. " '__reduce_ex__',\n",
  420. " '__repr__',\n",
  421. " '__setattr__',\n",
  422. " '__sizeof__',\n",
  423. " '__str__',\n",
  424. " '__subclasshook__',\n",
  425. " '__weakref__',\n",
  426. " 'n',\n",
  427. " 'v']"
  428. ]
  429. },
  430. "execution_count": 16,
  431. "metadata": {},
  432. "output_type": "execute_result"
  433. }
  434. ],
  435. "source": [
  436. "dir(eg1)"
  437. ]
  438. },
  439. {
  440. "cell_type": "code",
  441. "execution_count": 17,
  442. "metadata": {},
  443. "outputs": [
  444. {
  445. "name": "stdout",
  446. "output_type": "stream",
  447. "text": [
  448. "one 1\n",
  449. "two 2\n"
  450. ]
  451. }
  452. ],
  453. "source": [
  454. "print(eg1.n, eg1.v)\n",
  455. "print(eg2.n, eg2.v)"
  456. ]
  457. },
  458. {
  459. "cell_type": "markdown",
  460. "metadata": {},
  461. "source": [
  462. "现在我们解决了这个错误。现在让我们比较一下我们看到的两个例子。\n",
  463. "\n",
  464. "当我声明self.name和self.value,使用eg1.name和eg1.value没有属性错误。当我声明self.n和self.s时,使用eg1.n和eg1.s没有属性错误。\n",
  465. "\n",
  466. "从以上我们可以得出**self**就是实例本身。\n",
  467. "\n",
  468. "记住,**self**不是Python的关键词,它是用户定义的。你可以利用任何你觉得舒服的东西。但是使用self已经成为一种常见的做法。"
  469. ]
  470. },
  471. {
  472. "cell_type": "code",
  473. "execution_count": 17,
  474. "metadata": {},
  475. "outputs": [],
  476. "source": [
  477. "class FirstClass:\n",
  478. " def __init__(asdf1234,name,value):\n",
  479. " asdf1234.n = name\n",
  480. " asdf1234.v = value"
  481. ]
  482. },
  483. {
  484. "cell_type": "code",
  485. "execution_count": 18,
  486. "metadata": {},
  487. "outputs": [],
  488. "source": [
  489. "eg1 = FirstClass('one',1)\n",
  490. "eg2 = FirstClass('two',2)"
  491. ]
  492. },
  493. {
  494. "cell_type": "code",
  495. "execution_count": 19,
  496. "metadata": {},
  497. "outputs": [
  498. {
  499. "name": "stdout",
  500. "output_type": "stream",
  501. "text": [
  502. "one 1\n",
  503. "two 2\n"
  504. ]
  505. }
  506. ],
  507. "source": [
  508. "print(eg1.n, eg1.v)\n",
  509. "print(eg2.n, eg2.v)"
  510. ]
  511. },
  512. {
  513. "cell_type": "markdown",
  514. "metadata": {},
  515. "source": [
  516. "因为eg1和eg2是FirstClass的实例,所以它不需要被限制在FirstClass本身。它可以通过声明其他属性来扩展自己,而不需要在FirstClass中声明属性。"
  517. ]
  518. },
  519. {
  520. "cell_type": "code",
  521. "execution_count": 18,
  522. "metadata": {},
  523. "outputs": [],
  524. "source": [
  525. "eg1.cube = 1\n",
  526. "eg2.cube = 8"
  527. ]
  528. },
  529. {
  530. "cell_type": "code",
  531. "execution_count": 19,
  532. "metadata": {},
  533. "outputs": [
  534. {
  535. "data": {
  536. "text/plain": [
  537. "['__class__',\n",
  538. " '__delattr__',\n",
  539. " '__dict__',\n",
  540. " '__dir__',\n",
  541. " '__doc__',\n",
  542. " '__eq__',\n",
  543. " '__format__',\n",
  544. " '__ge__',\n",
  545. " '__getattribute__',\n",
  546. " '__gt__',\n",
  547. " '__hash__',\n",
  548. " '__init__',\n",
  549. " '__init_subclass__',\n",
  550. " '__le__',\n",
  551. " '__lt__',\n",
  552. " '__module__',\n",
  553. " '__ne__',\n",
  554. " '__new__',\n",
  555. " '__reduce__',\n",
  556. " '__reduce_ex__',\n",
  557. " '__repr__',\n",
  558. " '__setattr__',\n",
  559. " '__sizeof__',\n",
  560. " '__str__',\n",
  561. " '__subclasshook__',\n",
  562. " '__weakref__',\n",
  563. " 'cube',\n",
  564. " 'n',\n",
  565. " 'v']"
  566. ]
  567. },
  568. "execution_count": 19,
  569. "metadata": {},
  570. "output_type": "execute_result"
  571. }
  572. ],
  573. "source": [
  574. "dir(eg1)"
  575. ]
  576. },
  577. {
  578. "cell_type": "markdown",
  579. "metadata": {
  580. "collapsed": true
  581. },
  582. "source": [
  583. "就像我们前面看到的全局变量和局部变量一样,即使类也有自己的变量类型。\n",
  584. "\n",
  585. "**类属性**:在方法外部定义的属性,适用于所有实例。\n",
  586. "\n",
  587. "**实例属性**:在方法内部定义的属性,只适用于该方法,并且对每个实例都是唯一的。"
  588. ]
  589. },
  590. {
  591. "cell_type": "code",
  592. "execution_count": 20,
  593. "metadata": {},
  594. "outputs": [],
  595. "source": [
  596. "class FirstClass:\n",
  597. " test = 'test'\n",
  598. " def __init__(self,name,value):\n",
  599. " self.name = name\n",
  600. " self.value = value"
  601. ]
  602. },
  603. {
  604. "cell_type": "markdown",
  605. "metadata": {},
  606. "source": [
  607. "这里test是一个类属性,而name是一个实例属性。"
  608. ]
  609. },
  610. {
  611. "cell_type": "code",
  612. "execution_count": 22,
  613. "metadata": {},
  614. "outputs": [
  615. {
  616. "name": "stdout",
  617. "output_type": "stream",
  618. "text": [
  619. "test4\n"
  620. ]
  621. }
  622. ],
  623. "source": [
  624. "eg3 = FirstClass('Three',3)\n",
  625. "eg4 = FirstClass('Four', 4)\n",
  626. "eg4.test = 'test4'\n",
  627. "print(eg4.test)"
  628. ]
  629. },
  630. {
  631. "cell_type": "code",
  632. "execution_count": 23,
  633. "metadata": {},
  634. "outputs": [
  635. {
  636. "name": "stdout",
  637. "output_type": "stream",
  638. "text": [
  639. "test Three\n"
  640. ]
  641. }
  642. ],
  643. "source": [
  644. "print(eg3.test, eg3.name)"
  645. ]
  646. },
  647. {
  648. "cell_type": "markdown",
  649. "metadata": {},
  650. "source": [
  651. "让我们添加更多的方法到FirstClass。"
  652. ]
  653. },
  654. {
  655. "cell_type": "code",
  656. "execution_count": 24,
  657. "metadata": {},
  658. "outputs": [],
  659. "source": [
  660. "class FirstClass:\n",
  661. " def __init__(self,name,value):\n",
  662. " self.name = name\n",
  663. " self.value = value\n",
  664. " def square(self):\n",
  665. " return self.value * self.value\n",
  666. " def cube(self):\n",
  667. " return self.value * self.value * self.value\n",
  668. " def multiply(self, x):\n",
  669. " return self.value * x"
  670. ]
  671. },
  672. {
  673. "cell_type": "code",
  674. "execution_count": 25,
  675. "metadata": {},
  676. "outputs": [],
  677. "source": [
  678. "eg4 = FirstClass('Five',5)"
  679. ]
  680. },
  681. {
  682. "cell_type": "code",
  683. "execution_count": 26,
  684. "metadata": {},
  685. "outputs": [
  686. {
  687. "name": "stdout",
  688. "output_type": "stream",
  689. "text": [
  690. "25\n",
  691. "125\n"
  692. ]
  693. }
  694. ],
  695. "source": [
  696. "print(eg4.square())\n",
  697. "print(eg4.cube())"
  698. ]
  699. },
  700. {
  701. "cell_type": "code",
  702. "execution_count": 27,
  703. "metadata": {},
  704. "outputs": [
  705. {
  706. "data": {
  707. "text/plain": [
  708. "10"
  709. ]
  710. },
  711. "execution_count": 27,
  712. "metadata": {},
  713. "output_type": "execute_result"
  714. }
  715. ],
  716. "source": [
  717. "eg4.multiply(2)"
  718. ]
  719. },
  720. {
  721. "cell_type": "markdown",
  722. "metadata": {},
  723. "source": [
  724. "以上也可以写成:"
  725. ]
  726. },
  727. {
  728. "cell_type": "code",
  729. "execution_count": 28,
  730. "metadata": {},
  731. "outputs": [
  732. {
  733. "data": {
  734. "text/plain": [
  735. "10"
  736. ]
  737. },
  738. "execution_count": 28,
  739. "metadata": {},
  740. "output_type": "execute_result"
  741. }
  742. ],
  743. "source": [
  744. "FirstClass.multiply(eg4,2)"
  745. ]
  746. },
  747. {
  748. "cell_type": "markdown",
  749. "metadata": {},
  750. "source": [
  751. "## 2. 继承"
  752. ]
  753. },
  754. {
  755. "cell_type": "markdown",
  756. "metadata": {},
  757. "source": [
  758. "在某些情况下,新类需要具有已定义类的所有特征。因此,新类可以“继承”前一个类,并向其添加自己的方法,这称为继承。"
  759. ]
  760. },
  761. {
  762. "cell_type": "markdown",
  763. "metadata": {},
  764. "source": [
  765. "考虑类Person类具有薪水的方法。"
  766. ]
  767. },
  768. {
  769. "cell_type": "code",
  770. "execution_count": 29,
  771. "metadata": {},
  772. "outputs": [],
  773. "source": [
  774. "class Person:\n",
  775. " def __init__(self,name,age):\n",
  776. " self.name = name\n",
  777. " self.age = age\n",
  778. " def salary(self, value):\n",
  779. " self.money = value\n",
  780. " print(self.name,\"earns\",self.money)"
  781. ]
  782. },
  783. {
  784. "cell_type": "code",
  785. "execution_count": 30,
  786. "metadata": {},
  787. "outputs": [],
  788. "source": [
  789. "a = Person('Jerry',26)"
  790. ]
  791. },
  792. {
  793. "cell_type": "code",
  794. "execution_count": 31,
  795. "metadata": {},
  796. "outputs": [
  797. {
  798. "name": "stdout",
  799. "output_type": "stream",
  800. "text": [
  801. "Jerry earns 40000\n"
  802. ]
  803. }
  804. ],
  805. "source": [
  806. "a.salary(40000)"
  807. ]
  808. },
  809. {
  810. "cell_type": "code",
  811. "execution_count": 32,
  812. "metadata": {},
  813. "outputs": [
  814. {
  815. "data": {
  816. "text/plain": [
  817. "['__class__',\n",
  818. " '__delattr__',\n",
  819. " '__dict__',\n",
  820. " '__dir__',\n",
  821. " '__doc__',\n",
  822. " '__eq__',\n",
  823. " '__format__',\n",
  824. " '__ge__',\n",
  825. " '__getattribute__',\n",
  826. " '__gt__',\n",
  827. " '__hash__',\n",
  828. " '__init__',\n",
  829. " '__init_subclass__',\n",
  830. " '__le__',\n",
  831. " '__lt__',\n",
  832. " '__module__',\n",
  833. " '__ne__',\n",
  834. " '__new__',\n",
  835. " '__reduce__',\n",
  836. " '__reduce_ex__',\n",
  837. " '__repr__',\n",
  838. " '__setattr__',\n",
  839. " '__sizeof__',\n",
  840. " '__str__',\n",
  841. " '__subclasshook__',\n",
  842. " '__weakref__',\n",
  843. " 'salary']"
  844. ]
  845. },
  846. "execution_count": 32,
  847. "metadata": {},
  848. "output_type": "execute_result"
  849. }
  850. ],
  851. "source": [
  852. "dir(Person)"
  853. ]
  854. },
  855. {
  856. "cell_type": "markdown",
  857. "metadata": {},
  858. "source": [
  859. "现在考虑另一个Artist类,告诉我们艺术家挣的钱的数量和他的艺术形式。"
  860. ]
  861. },
  862. {
  863. "cell_type": "code",
  864. "execution_count": 34,
  865. "metadata": {},
  866. "outputs": [],
  867. "source": [
  868. "class Artist:\n",
  869. " def __init__(self,name,age):\n",
  870. " self.name = name\n",
  871. " self.age = age\n",
  872. " def salary(self,value):\n",
  873. " self.money = value\n",
  874. " print(self.name,\"earns\",self.money)\n",
  875. " def artform(self, job):\n",
  876. " self.job = job\n",
  877. " print(self.name,\"is a\", self.job)"
  878. ]
  879. },
  880. {
  881. "cell_type": "code",
  882. "execution_count": 35,
  883. "metadata": {},
  884. "outputs": [],
  885. "source": [
  886. "b = Artist('Nick',20)"
  887. ]
  888. },
  889. {
  890. "cell_type": "code",
  891. "execution_count": 36,
  892. "metadata": {},
  893. "outputs": [
  894. {
  895. "name": "stdout",
  896. "output_type": "stream",
  897. "text": [
  898. "Nick earns 50000\n",
  899. "Nick is a Musician\n"
  900. ]
  901. }
  902. ],
  903. "source": [
  904. "b.salary(50000)\n",
  905. "b.artform('Musician')"
  906. ]
  907. },
  908. {
  909. "cell_type": "code",
  910. "execution_count": 37,
  911. "metadata": {},
  912. "outputs": [
  913. {
  914. "data": {
  915. "text/plain": [
  916. "['__class__',\n",
  917. " '__delattr__',\n",
  918. " '__dict__',\n",
  919. " '__dir__',\n",
  920. " '__doc__',\n",
  921. " '__eq__',\n",
  922. " '__format__',\n",
  923. " '__ge__',\n",
  924. " '__getattribute__',\n",
  925. " '__gt__',\n",
  926. " '__hash__',\n",
  927. " '__init__',\n",
  928. " '__init_subclass__',\n",
  929. " '__le__',\n",
  930. " '__lt__',\n",
  931. " '__module__',\n",
  932. " '__ne__',\n",
  933. " '__new__',\n",
  934. " '__reduce__',\n",
  935. " '__reduce_ex__',\n",
  936. " '__repr__',\n",
  937. " '__setattr__',\n",
  938. " '__sizeof__',\n",
  939. " '__str__',\n",
  940. " '__subclasshook__',\n",
  941. " '__weakref__',\n",
  942. " 'artform',\n",
  943. " 'salary']"
  944. ]
  945. },
  946. "execution_count": 37,
  947. "metadata": {},
  948. "output_type": "execute_result"
  949. }
  950. ],
  951. "source": [
  952. "dir(Artist)"
  953. ]
  954. },
  955. {
  956. "cell_type": "markdown",
  957. "metadata": {},
  958. "source": [
  959. "money 方法和salary 方法是一样的。因此,我们可以将该方法推广到工资类,并将软件工程师类继承到美术师类。现在艺术类变成了,"
  960. ]
  961. },
  962. {
  963. "cell_type": "code",
  964. "execution_count": 38,
  965. "metadata": {},
  966. "outputs": [],
  967. "source": [
  968. "class Artist(Person):\n",
  969. " def artform(self, job):\n",
  970. " self.job = job\n",
  971. " print(self.name,\"is a\", self.job)"
  972. ]
  973. },
  974. {
  975. "cell_type": "code",
  976. "execution_count": 40,
  977. "metadata": {},
  978. "outputs": [],
  979. "source": [
  980. "c = Artist('Tom',21)"
  981. ]
  982. },
  983. {
  984. "cell_type": "code",
  985. "execution_count": 41,
  986. "metadata": {},
  987. "outputs": [
  988. {
  989. "data": {
  990. "text/plain": [
  991. "['__class__',\n",
  992. " '__delattr__',\n",
  993. " '__dict__',\n",
  994. " '__dir__',\n",
  995. " '__doc__',\n",
  996. " '__eq__',\n",
  997. " '__format__',\n",
  998. " '__ge__',\n",
  999. " '__getattribute__',\n",
  1000. " '__gt__',\n",
  1001. " '__hash__',\n",
  1002. " '__init__',\n",
  1003. " '__init_subclass__',\n",
  1004. " '__le__',\n",
  1005. " '__lt__',\n",
  1006. " '__module__',\n",
  1007. " '__ne__',\n",
  1008. " '__new__',\n",
  1009. " '__reduce__',\n",
  1010. " '__reduce_ex__',\n",
  1011. " '__repr__',\n",
  1012. " '__setattr__',\n",
  1013. " '__sizeof__',\n",
  1014. " '__str__',\n",
  1015. " '__subclasshook__',\n",
  1016. " '__weakref__',\n",
  1017. " 'artform',\n",
  1018. " 'salary']"
  1019. ]
  1020. },
  1021. "execution_count": 41,
  1022. "metadata": {},
  1023. "output_type": "execute_result"
  1024. }
  1025. ],
  1026. "source": [
  1027. "dir(Artist)"
  1028. ]
  1029. },
  1030. {
  1031. "cell_type": "code",
  1032. "execution_count": 42,
  1033. "metadata": {},
  1034. "outputs": [
  1035. {
  1036. "name": "stdout",
  1037. "output_type": "stream",
  1038. "text": [
  1039. "Tom earns 60000\n",
  1040. "Tom is a Dancer\n"
  1041. ]
  1042. }
  1043. ],
  1044. "source": [
  1045. "c.salary(60000)\n",
  1046. "c.artform('Dancer')"
  1047. ]
  1048. },
  1049. {
  1050. "cell_type": "markdown",
  1051. "metadata": {},
  1052. "source": [
  1053. "假设在继承一个特定方法的时候,该方法不适合新类。可以通过在新类中用相同的名称再次定义该方法来重写该方法。"
  1054. ]
  1055. },
  1056. {
  1057. "cell_type": "code",
  1058. "execution_count": 44,
  1059. "metadata": {},
  1060. "outputs": [],
  1061. "source": [
  1062. "class Artist(Person):\n",
  1063. " def artform(self, job):\n",
  1064. " self.job = job\n",
  1065. " print(self.name,\"is a\", self.job)\n",
  1066. " def salary(self, value):\n",
  1067. " self.money = value\n",
  1068. " print(self.name,\"earns\",self.money)\n",
  1069. " print(\"I am overriding the SoftwareEngineer class's salary method\")"
  1070. ]
  1071. },
  1072. {
  1073. "cell_type": "code",
  1074. "execution_count": 45,
  1075. "metadata": {},
  1076. "outputs": [],
  1077. "source": [
  1078. "c = Artist('Tom',21)"
  1079. ]
  1080. },
  1081. {
  1082. "cell_type": "code",
  1083. "execution_count": 46,
  1084. "metadata": {},
  1085. "outputs": [
  1086. {
  1087. "name": "stdout",
  1088. "output_type": "stream",
  1089. "text": [
  1090. "Tom earns 60000\n",
  1091. "I am overriding the SoftwareEngineer class's salary method\n",
  1092. "Tom is a Dancer\n"
  1093. ]
  1094. }
  1095. ],
  1096. "source": [
  1097. "c.salary(60000)\n",
  1098. "c.artform('Dancer')"
  1099. ]
  1100. },
  1101. {
  1102. "cell_type": "markdown",
  1103. "metadata": {},
  1104. "source": [
  1105. "如果输入参数的数量因实例而异,则可以使用星号。"
  1106. ]
  1107. },
  1108. {
  1109. "cell_type": "code",
  1110. "execution_count": 47,
  1111. "metadata": {},
  1112. "outputs": [],
  1113. "source": [
  1114. "class NotSure:\n",
  1115. " def __init__(self, *args):\n",
  1116. " self.data = ' '.join(list(args)) "
  1117. ]
  1118. },
  1119. {
  1120. "cell_type": "code",
  1121. "execution_count": 48,
  1122. "metadata": {},
  1123. "outputs": [],
  1124. "source": [
  1125. "yz = NotSure('I', 'Do' , 'Not', 'Know', 'What', 'To','Type')"
  1126. ]
  1127. },
  1128. {
  1129. "cell_type": "code",
  1130. "execution_count": 49,
  1131. "metadata": {},
  1132. "outputs": [
  1133. {
  1134. "data": {
  1135. "text/plain": [
  1136. "'I Do Not Know What To Type'"
  1137. ]
  1138. },
  1139. "execution_count": 49,
  1140. "metadata": {},
  1141. "output_type": "execute_result"
  1142. }
  1143. ],
  1144. "source": [
  1145. "yz.data"
  1146. ]
  1147. },
  1148. {
  1149. "cell_type": "markdown",
  1150. "metadata": {},
  1151. "source": [
  1152. "# 3. 接下来应该怎么做"
  1153. ]
  1154. },
  1155. {
  1156. "cell_type": "markdown",
  1157. "metadata": {},
  1158. "source": [
  1159. "为了学好Python,仅仅看教程是不够的,需要做大量的练习题。可以使用教程里所列的练习题,也可以自己找各个方面的练习题。\n",
  1160. "\n",
  1161. "* 编程比较重要的培养编程思维,如果抄别人写好的代码,发现不了Python的窍门、技巧,因此需要独立自主完成编程练习,也可以给自己出一些小项目,并解决它们。\n",
  1162. "* 你编写的代码越多,你发现的越多,你就越开始欣赏这门语言。\n",
  1163. "* 强烈建议把[《Python作业》](https://gitee.com/pi-lab/machinelearning_homework/blob/master/homework_01_python/README.md)完成\n",
  1164. "* 在完成基本的编程习题之后,可以在[《其他编程练习》](https://gitee.com/pi-lab/machinelearning_homework/blob/master/homework_01_python/README.md#references)里面找一些练习题或者项目做一下。\n",
  1165. "\n",
  1166. "现在已经介绍了Python,可以尝试感兴趣的领域中的不同Python库。强烈建议查看这个Python框架、库和软件列表 http://awesome-python.com\n",
  1167. "\n",
  1168. "\n",
  1169. "Python 教程:\n",
  1170. "* [Python tutorial (廖雪峰)](https://www.liaoxuefeng.com/wiki/1016959663602400)\n",
  1171. "* [Python基础教程](https://www.runoob.com/python/python-tutorial.html)\n",
  1172. "* [Python官方教程(中文版)](https://docs.python.org/zh-cn/3/tutorial/index.html)\n",
  1173. "* [Python官方文档](https://docs.python.org/3/)\n",
  1174. "* [跟海龟学Python](https://gitee.com/pi-lab/python_turtle)\n"
  1175. ]
  1176. },
  1177. {
  1178. "cell_type": "markdown",
  1179. "metadata": {},
  1180. "source": [
  1181. "## **最后,享受解决问题的快乐!因为生命短暂,你需要Python!**"
  1182. ]
  1183. }
  1184. ],
  1185. "metadata": {
  1186. "kernelspec": {
  1187. "display_name": "Python 3",
  1188. "language": "python",
  1189. "name": "python3"
  1190. },
  1191. "language_info": {
  1192. "codemirror_mode": {
  1193. "name": "ipython",
  1194. "version": 3
  1195. },
  1196. "file_extension": ".py",
  1197. "mimetype": "text/x-python",
  1198. "name": "python",
  1199. "nbconvert_exporter": "python",
  1200. "pygments_lexer": "ipython3",
  1201. "version": "3.7.9"
  1202. }
  1203. },
  1204. "nbformat": 4,
  1205. "nbformat_minor": 1
  1206. }

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。