日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

编程基础python学习2完结

發(fā)布時(shí)間:2024/4/17 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编程基础python学习2完结 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

What is a Class?

Data structures like lists and strings are extremely useful, but sometimes they aren’t enough to represent something you’re trying to implement. For example, let’s say we needed to keep track of a bunch of pets. We could represent a pet using a list by specifying the first element of the list as the pet’s name and the second element of the list as the pet’s species. This is very arbitrary and nonintuitive, however – how do you know which element is supposed to be which?

Classes give us the ability to create more complicated data structures that contain arbitrary content. We can create a?Pet?class that keeps track of the name and species of the pet in usefully named attributes called?name?and?species, respectively.

What is an Instance?

Before we get into creating a class itself, we need to understand an important distinction. A class is something that just contains structure – it defines how something should be laid out or structured, but doesn’t actually fill in the content. For example, a?Pet?class may say that a pet needs to have a name and a species, but it will not actually say what the pet’s name or species is.

This is where?instances?come in. An instance is a specific copy of the class that does contain all of the content. For example, if I create a pet?polly, with name?"Polly"?and species?"Parrot", then?polly?is an instance of?Pet.

This can sometimes be a very difficult concept to master, so let’s look at it from another angle. Let’s say that the government has a particular tax form that it requires everybody to fill out. Everybody has to fill out the same?type?of form, but the content that people put into the form differs from person to person. A?classis like the form: it specifies what content should exist. Your copy of the form with your specific information if like an?instance?of the class: it specifies what the content actually is.

?

?

This is the basic instant for creating a class. The first word,?class, indicates that we are creating a class. The second word,?Pet, indicates the name of the class. The word in parentheses,?object, is the class thatPet?is inheriting from. We’ll get more into inheritance below, so for now all you need to know is that?object?is a special variable in Python that you should include in the parentheses when you are creating a new class.

class表示我們要新建一個(gè)類

pet是新建的類名

object是在python里一個(gè)特殊的變量,需要包含在新建類的圓括號(hào)里(知道怎么用就行了)

?http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/

?

?movie_storyline和poster_image等是初始化函數(shù)的自變量,而self.title,self.storyline等是實(shí)例變量。

self.title=movie_title就是在用函數(shù)的自變量初始化實(shí)例變量。

對(duì)比,在定義新類和定義初始化函數(shù)的括號(hào)后面少了冒號(hào):

對(duì)比,在調(diào)用Media.movie時(shí),實(shí)際上是在調(diào)用init函數(shù),而init函數(shù)里的self就是類的一個(gè)實(shí)例,在此處就是指向toy_story.問題出在此處沒有給init函數(shù)里的除self其他各個(gè)參數(shù)賦值,當(dāng)你需要print toy_story時(shí)程序不知道此值是什么應(yīng)該輸出什么所以此處會(huì)報(bào)錯(cuò)。

?

?

一旦init函數(shù)被調(diào)用,并且media.movie里的四個(gè)自變量獲得了正確的值,所有與toy_story相關(guān)的變量都會(huì)被適當(dāng)?shù)爻跏蓟R虼水?dāng)我們需要程序打印出我們需要的值時(shí)我們就能正確得到。

?

當(dāng)我們創(chuàng)建好實(shí)例后,后臺(tái)真正的操作是為我們的每一個(gè)實(shí)例創(chuàng)建一定的空間,在空間內(nèi) 每個(gè)實(shí)例有自己的變量副本

Like this

?

由于關(guān)于類movie的每個(gè)實(shí)例都是唯一的,所以這些變量為“實(shí)例變量”

?此時(shí)如果把init函數(shù)中movie_storyline賦值語句中的self刪掉,那么如果print(toy_story.movie_storyline)會(huì)導(dǎo)致調(diào)用init函數(shù)的時(shí)候報(bào)錯(cuò)toy_story沒有movie_storyline這個(gè)屬性。

?

在類中定義,并與實(shí)例相關(guān)的函數(shù):實(shí)例方法

例如init()就是一個(gè)實(shí)例方法。并且每一個(gè)實(shí)例方法的第一個(gè)自變量都是self。

show_trailer是另外一個(gè)實(shí)例方法。打開的鏈接就儲(chǔ)存在self.trailer_youtube_url中,所以要訪問這個(gè)實(shí)例變量。

然后再在avatar這個(gè)實(shí)例中調(diào)用這個(gè)實(shí)例方法即可。

?

練習(xí):

?

此處應(yīng)該是調(diào)用的webbrowser的open函數(shù)而不是簡單的一個(gè)名字所以應(yīng)該是點(diǎn)·而不是下劃線_

?

術(shù)語總結(jié):

一個(gè)新的知識(shí)點(diǎn):

fresh_tomatoes這個(gè)文件中有一個(gè)open_movies_page的函數(shù)

?這個(gè)函數(shù)輸入一個(gè)電影列表,會(huì)創(chuàng)建并輸出一個(gè)html文件或網(wǎng)頁來顯示這些電影。(好牛杯!厲害了我的小西紅柿...)

但是有一個(gè)重要前提是這個(gè)西紅柿文件必須和你其他所有電影程序儲(chǔ)存在同一個(gè)文件夾內(nèi)才可以起作用。

?

面向?qū)ο缶幊讨械母呒?jí)概念

1.類變量(所有同類的實(shí)例都相同的變量)定義在類中。(類變量可能是個(gè)常量,python指南中說當(dāng)它為常量最好用全大寫。)like this.

?

三個(gè)引號(hào)表示可以在多行創(chuàng)建文檔。

?

就可以打印出打引號(hào)的內(nèi)容。

注意用法print(模塊或者文件.類名和預(yù)定義的類變量)

?

面向?qū)ο缶幊痰囊淮髢?yōu)勢:reuse the code

?

劃線的語法說明child類可以繼承或重用所有parent類公開可用的所有東西。

?

要初始化所有從parent類那里繼承的變量,例如last_name和eye_color,實(shí)際上要重用類parent中init方法,like this:

而child類自己的實(shí)例變量在后面初始化。

高亮處為創(chuàng)建一個(gè)child類的實(shí)例,當(dāng)運(yùn)行這條代碼的時(shí)候child.__init__初始化函數(shù)會(huì)被立刻調(diào)用。

整個(gè)程序后臺(tái)運(yùn)行的過程:

運(yùn)行child類的實(shí)例——>child__init__函數(shù)被調(diào)用——>init函數(shù)第一行就是打印所以先打印出Child Constuctor called——>parent類的init函數(shù)被調(diào)用,所以程序從4到5class parent定義處,parent__init__被調(diào)用——>Parent Constructor called被打印——>parent類的實(shí)例變量last_name和eye_color被初始化,類parent.__init__函數(shù)成功運(yùn)行后——>控制回到4處,然后到8child類的實(shí)例變量被初始化。child類的init函數(shù)被成功運(yùn)行——>child實(shí)例miley_cyrus被成功創(chuàng)建。——>接下來到9按順序打印出需要打印的內(nèi)容。

like this:

?

新知識(shí)點(diǎn):重寫函數(shù)(方法)

雖然在child類中沒有明確地定義方法show_info但是child從parent那里繼承了所有公開可用的信息包括show_info方法,所以可以直接用。結(jié)果like this,直接調(diào)用了parent類中定義的show_info方法。

?

?

新知識(shí):方法覆蓋(method overriding)

?

此時(shí)在child類中再定義和parent類同名的方法show_info,當(dāng)調(diào)用這個(gè)方法時(shí),child類的方法會(huì)覆蓋掉parent類的方法。結(jié)果like this

輸出了child類中定義的show_info方法中的內(nèi)容。

?

轉(zhuǎn)載于:https://www.cnblogs.com/Yiren-33/p/6689491.html

總結(jié)

以上是生活随笔為你收集整理的编程基础python学习2完结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。