mysql解壓版本怎么用,mysql57解壓版安裝教程

簡(jiǎn)述python函數(shù)中參數(shù)的幾種形態(tài)

在調(diào)用函數(shù)時(shí),通常會(huì)傳遞參數(shù),函數(shù)內(nèi)部的代碼保持不變,針對(duì) 不同的參數(shù)處理不同的數(shù)據(jù)。

創(chuàng)新互聯(lián)主營(yíng)榆陽(yáng)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app軟件開(kāi)發(fā),榆陽(yáng)h5小程序制作搭建,榆陽(yáng)網(wǎng)站營(yíng)銷推廣歡迎榆陽(yáng)等地區(qū)企業(yè)咨詢

有位置傳參、關(guān)鍵字傳參、默認(rèn)值參數(shù)、多值參數(shù)等。

1、參數(shù)傳遞

形參和實(shí)參:

形參:定義 函數(shù)時(shí)的 參數(shù)變量

實(shí)參:調(diào)用 函數(shù)時(shí),使用的參數(shù)變量

參數(shù)傳遞的過(guò)程,就是 把實(shí)參的引用 傳遞給 形參 ,使用實(shí)參的值來(lái)執(zhí)行函數(shù)體的過(guò)程。

在 Python 中,函數(shù)的 實(shí)參/返回值 都是是靠 引用 來(lái)傳遞來(lái)的

2、位置實(shí)參

按照參數(shù)位置,依次傳遞參數(shù),這是最普通的方式。

?

如何正確地使用Python的屬性和描述符

關(guān)于@property裝飾器

在Python中我們使用@property裝飾器來(lái)把對(duì)函數(shù)的調(diào)用偽裝成對(duì)屬性的訪問(wèn)。

那么為什么要這樣做呢?因?yàn)锧property讓我們將自定義的代碼同變量的訪問(wèn)/設(shè)定聯(lián)系在了一起,同時(shí)為你的類保持一個(gè)簡(jiǎn)單的訪問(wèn)屬性的接口。

舉個(gè)栗子,假如我們有一個(gè)需要表示電影的類:

1

2

3

4

5

6

7

8

class Movie(object):

def __init__(self, title, description, score, ticket):

self.title = title

self.description = description

self.score = scroe

self.ticket = ticket

你開(kāi)始在項(xiàng)目的其他地方使用這個(gè)類,但是之后你意識(shí)到:如果不小心給電影打了負(fù)分怎么辦?你覺(jué)得這是錯(cuò)誤的行為,希望Movie類可以阻止這個(gè)錯(cuò)誤。 你首先想到的辦法是將Movie類修改為這樣:

Python

1

2

3

4

5

6

7

8

class Movie(object):

def __init__(self, title, description, score, ticket):

self.title = title

self.description = description

self.ticket = ticket

if score 0:

raise ValueError("Negative value not allowed:{}".format(score))

self.score = scroe

但這行不通。因?yàn)槠渌糠值拇a都是直接通過(guò)Movie.score來(lái)賦值的。這個(gè)新修改的類只會(huì)在__init__方法中捕獲錯(cuò)誤的數(shù)據(jù),但對(duì)于已經(jīng)存在的類實(shí)例就無(wú)能為力了。如果有人試著運(yùn)行m.scrore= -100,那么誰(shuí)也沒(méi)法阻止。那該怎么辦?

Python的property解決了這個(gè)問(wèn)題。

我們可以這樣做

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

class Movie(object):

def __init__(self, title, description, score):

self.title = title

self.description = description

self.score = score

self.ticket = ticket

@property

def score(self):

return self.__score

@score.setter

def score(self, score):

if score 0:

raise ValueError("Negative value not allowed:{}".format(score))

self.__score = score

@score.deleter

def score(self):

raise AttributeError("Can not delete score")

這樣在任何地方修改score都會(huì)檢測(cè)它是否小于0。

property的不足

對(duì)property來(lái)說(shuō),最大的缺點(diǎn)就是它們不能重復(fù)使用。舉個(gè)例子,假設(shè)你想為ticket字段也添加非負(fù)檢查。下面是修改過(guò)的新類:

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

class Movie(object):

def __init__(self, title, description, score, ticket):

self.title = title

self.description = description

self.score = score

self.ticket = ticket

@property

def score(self):

return self.__score

@score.setter

def score(self, score):

if score 0:

raise ValueError("Negative value not allowed:{}".format(score))

self.__score = score

@score.deleter

def score(self):

raise AttributeError("Can not delete score")

@property

def ticket(self):

return self.__ticket

@ticket.setter

def ticket(self, ticket):

if ticket 0:

raise ValueError("Negative value not allowed:{}".format(ticket))

self.__ticket = ticket

@ticket.deleter

def ticket(self):

raise AttributeError("Can not delete ticket")

可以看到代碼增加了不少,但重復(fù)的邏輯也出現(xiàn)了不少。雖然property可以讓類從外部看起來(lái)接口整潔漂亮,但是卻做不到內(nèi)部同樣整潔漂亮。

描述符登場(chǎng)

什么是描述符?

一般來(lái)說(shuō),描述符是一個(gè)具有綁定行為的對(duì)象屬性,其屬性的訪問(wèn)被描述符協(xié)議方法覆寫。這些方法是__get__()、__set__()和__delete__(),一個(gè)對(duì)象中只要包含了這三個(gè)方法中的至少一個(gè)就稱它為描述符。

描述符有什么作用?

The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance, a.x has a lookup chain starting witha.__dict__[‘x’], then type(a).__dict__[‘x’], and continuing through the base classes of type(a) excluding metaclasses. If the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined.—–摘自官方文檔

簡(jiǎn)單的說(shuō)描述符會(huì)改變一個(gè)屬性的基本的獲取、設(shè)置和刪除方式。

先看如何用描述符來(lái)解決上面 property邏輯重復(fù)的問(wèn)題。

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

class Integer(object):

def __init__(self, name):

self.name = name

def __get__(self, instance, owner):

return instance.__dict__[self.name]

def __set__(self, instance, value):

if value 0:

raise ValueError("Negative value not allowed")

instance.__dict__[self.name] = value

class Movie(object):

score = Integer('score')

ticket = Integer('ticket')

因?yàn)槊枋龇麅?yōu)先級(jí)高并且會(huì)改變默認(rèn)的get、set行為,這樣一來(lái),當(dāng)我們?cè)L問(wèn)或者設(shè)置Movie().score的時(shí)候都會(huì)受到描述符Integer的限制。

不過(guò)我們也總不能用下面這樣的方式來(lái)創(chuàng)建實(shí)例。

a = Movie()

a.score = 1

a.ticket = 2

a.title = ‘test’

a.descript = ‘…’

這樣太生硬了,所以我們還缺一個(gè)構(gòu)造函數(shù)。

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

class Integer(object):

def __init__(self, name):

self.name = name

def __get__(self, instance, owner):

if instance is None:

return self

return instance.__dict__[self.name]

def __set__(self, instance, value):

if value 0:

raise ValueError('Negative value not allowed')

instance.__dict__[self.name] = value

class Movie(object):

score = Integer('score')

ticket = Integer('ticket')

def __init__(self, title, description, score, ticket):

self.title = title

self.description = description

self.score = score

self.ticket = ticket

這樣在獲取、設(shè)置和刪除score和ticket的時(shí)候都會(huì)進(jìn)入Integer的__get__、__set__,從而減少了重復(fù)的邏輯。

現(xiàn)在雖然問(wèn)題得到了解決,但是你可能會(huì)好奇這個(gè)描述符到底是如何工作的。具體來(lái)說(shuō),在__init__函數(shù)里訪問(wèn)的是自己的self.score和self.ticket,怎么和類屬性score和ticket關(guān)聯(lián)起來(lái)的?

描述符如何工作

看官方的說(shuō)明

If an object defines both __get__() and __set__(), it is considered a data descriptor. Descriptors that only define __get__() are called non-data descriptors (they are typically used for methods but other uses are possible).

Data and non-data descriptors differ in how overrides are calculated with respect to entries in an instance’s dictionary. If an instance’s dictionary has an entry with the same name as a data descriptor, the data descriptor takes precedence. If an instance’s dictionary has an entry with the same name as a non-data descriptor, the dictionary entry takes precedence.

The important points to remember are:

descriptors are invoked by the __getattribute__() method

overriding __getattribute__() prevents automatic descriptor calls

object.__getattribute__() and type.__getattribute__() make different calls to __get__().

data descriptors always override instance dictionaries.

non-data descriptors may be overridden by instance dictionaries.

類調(diào)用__getattribute__()的時(shí)候大概是下面這樣子:

1

2

3

4

5

6

7

def __getattribute__(self, key):

"Emulate type_getattro() in Objects/typeobject.c"

v = object.__getattribute__(self, key)

if hasattr(v, '__get__'):

return v.__get__(None, self)

return v

下面是摘自國(guó)外一篇博客上的內(nèi)容。

Given a Class “C” and an Instance “c” where “c = C(…)”, calling “c.name” means looking up an Attribute “name” on the Instance “c” like this:

Get the Class from Instance

Call the Class’s special method getattribute__. All objects have a default __getattribute

Inside getattribute

Get the Class’s mro as ClassParents

For each ClassParent in ClassParents

If the Attribute is in the ClassParent’s dict

If is a data descriptor

Return the result from calling the data descriptor’s special method __get__()

Break the for each (do not continue searching the same Attribute any further)

If the Attribute is in Instance’s dict

Return the value as it is (even if the value is a data descriptor)

For each ClassParent in ClassParents

If the Attribute is in the ClassParent’s dict

If is a non-data descriptor

Return the result from calling the non-data descriptor’s special method __get__()

If it is NOT a descriptor

Return the value

If Class has the special method getattr

Return the result from calling the Class’s special method__getattr__.

我對(duì)上面的理解是,訪問(wèn)一個(gè)實(shí)例的屬性的時(shí)候是先遍歷它和它的父類,尋找它們的__dict__里是否有同名的data descriptor如果有,就用這個(gè)data descriptor代理該屬性,如果沒(méi)有再尋找該實(shí)例自身的__dict__,如果有就返回。任然沒(méi)有再查找它和它父類里的non-data descriptor,最后查找是否有__getattr__

描述符的應(yīng)用場(chǎng)景

python的property、classmethod修飾器本身也是一個(gè)描述符,甚至普通的函數(shù)也是描述符(non-data discriptor)

django model和SQLAlchemy里也有描述符的應(yīng)用

Python

1

2

3

4

5

6

7

8

9

10

11

12

class User(db.Model):

id = db.Column(db.Integer, primary_key=True)

username = db.Column(db.String(80), unique=True)

email = db.Column(db.String(120), unique=True)

def __init__(self, username, email):

self.username = username

self.email = email

def __repr__(self):

return 'User %r' % self.username

后記

只有當(dāng)確實(shí)需要在訪問(wèn)屬性的時(shí)候完成一些額外的處理任務(wù)時(shí),才應(yīng)該使用property。不然代碼反而會(huì)變得更加啰嗦,而且這樣會(huì)讓程序變慢很多。

python 關(guān)于函數(shù)的語(yǔ)法

這里的QuickSort.count叫做"函數(shù)屬性function attribute",

python等動(dòng)態(tài)類型語(yǔ)言所具有的"函數(shù)同時(shí)是頭等對(duì)象"的功能.

即代碼可以往函數(shù)對(duì)象上靈活地添加某屬性。

def?f():???

print(f.act)

f.act=123?#定義和添加一個(gè)函數(shù)對(duì)象的屬性-函數(shù)屬性

f()?#打印123

之前的快速排序用了一個(gè)count屬性在記錄排序算法的比較次數(shù)。屬于調(diào)試顯示,不是排序的核心算法..

二級(jí)Python----Python的內(nèi)置函數(shù)及標(biāo)準(zhǔn)庫(kù)(DAY 8)

python的內(nèi)置函數(shù)(68個(gè))

Python考核31個(gè)內(nèi)置函數(shù),

python內(nèi)置了很多內(nèi)置函數(shù)、類方法屬性及各種模塊。當(dāng)我們想要當(dāng)我們想要了解某種類型有哪些屬性方法以及每種方法該怎么使用時(shí),我們可以使用dir()函數(shù)和help()函數(shù)在python idle交互式模式下獲得我們想要的信息。

? dir()函數(shù)獲得對(duì)象中可用屬性的列表

Python中的關(guān)鍵詞有哪些?

dir(__builtins__):查看python內(nèi)置函數(shù)

help(‘keywords‘):查看python關(guān)鍵詞

如微分積分方程的求解程序、訪問(wèn)互聯(lián)網(wǎng)、獲取日期和時(shí)間、機(jī)器學(xué)習(xí)算法等。這些程序往往被收入程序庫(kù)中,構(gòu)成程序庫(kù)。

只有經(jīng)過(guò)嚴(yán)格檢驗(yàn)的程序才能放在程序庫(kù)里。檢驗(yàn),就是對(duì)程序作充分的測(cè)試。通常進(jìn)行的有正確性測(cè)試、精度測(cè)試、速度測(cè)試、邊界條件和出錯(cuò)狀態(tài)的測(cè)試。經(jīng)過(guò)檢驗(yàn)的程序不但能保證計(jì)算結(jié)果的正確性,而且對(duì)錯(cuò)誤調(diào)用也能作出反應(yīng)。程序庫(kù)中的程序都是規(guī)范化的。所謂規(guī)范化有三重含義:①同一庫(kù)里所有程序的格式是統(tǒng)一的;② 對(duì)這些程序的調(diào)用方法是相同的;③ 每個(gè)程序所需參數(shù)的數(shù)目、順序和類型都是嚴(yán)格規(guī)定好的。

Python的庫(kù)包含標(biāo)準(zhǔn)庫(kù)和第三方庫(kù)

標(biāo)準(zhǔn)庫(kù):程序語(yǔ)言自身?yè)碛械膸?kù),可以直接使用。help('modules')

第三方庫(kù):第三方者使用該語(yǔ)言提供的程序庫(kù)。

標(biāo)準(zhǔn)庫(kù): turtle 庫(kù)(必選)、 random 庫(kù)(必選)、 time 庫(kù)(可選)。

? turtle 庫(kù):圖形繪制庫(kù)

原理如同控制一只海龜,以不同的方向和速度進(jìn)行位移而得到其運(yùn)動(dòng)軌跡。

使用模塊的幫助時(shí),需要先將模塊導(dǎo)入。

例如:在IDLE中輸入import turtle

dir(turtle)

help(turtle.**)

1.畫布

畫布就是turtle為我們展開(kāi)用于繪圖區(qū)域, 我們可以設(shè)置它的大小和初始位置。

setup()方法用于初始化畫布窗口大小和位置,參數(shù)包括畫布窗口寬、畫布窗口高、窗口在屏幕的水平起始位置和窗口在屏幕的垂直起始位置。

參數(shù):width, height: 輸入寬和高為整數(shù)時(shí),表示 像素 ;為小數(shù)時(shí),表示占據(jù)電腦屏幕的比例。(startx,starty):這一坐標(biāo)表示

矩形窗口左上角頂點(diǎn)的位置,如果為空,則窗口位于屏幕中心:

例如:setup(640,480,300,300)表示在桌面屏幕(300,300)位置開(kāi)始創(chuàng)建640×480大小的畫布窗體。

2、畫筆

? color() 用于設(shè)置或返回畫筆顏色和填充顏色。

例如:color(‘red’)將顏色設(shè)為紅色,也可用fillcolor()方法設(shè)置或返回填充顏色,或用pencolor()方法設(shè)置或返回筆觸顏色。

python的內(nèi)置函數(shù)有哪些,都是什么意思?

print-輸出,input-輸入,int-將字符串轉(zhuǎn)數(shù)字(字符串必須是數(shù)字),str-將數(shù)字轉(zhuǎn)為字符串,list-將字符串/數(shù)字轉(zhuǎn)為列表,for-有限循環(huán),while-無(wú)限循環(huán)……………………………………

分享標(biāo)題:mysql解壓版本怎么用,mysql57解壓版安裝教程
當(dāng)前地址:http://muchs.cn/article42/hcgoec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google企業(yè)建站、網(wǎng)站改版小程序開(kāi)發(fā)、外貿(mào)網(wǎng)站建設(shè)商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站