mysql解压版本怎么用,mysql57解压版安装教程

简述python函数中参数的几种形态

在调用函数时,通常会传递参数,函数内部的代码保持不变,针对 不同的参数处理不同的数据。

创新互联主营榆阳网站建设的网络公司,主营网站建设方案,成都app软件开发,榆阳h5小程序制作搭建,榆阳网站营销推广欢迎榆阳等地区企业咨询

有位置传参、关键字传参、默认值参数、多值参数等。

1、参数传递

形参和实参:

形参:定义 函数时的 参数变量

实参:调用 函数时,使用的参数变量

参数传递的过程,就是 把实参的引用 传递给 形参 ,使用实参的值来执行函数体的过程。

在 Python 中,函数的 实参/返回值 都是是靠 引用 来传递来的

2、位置实参

按照参数位置,依次传递参数,这是最普通的方式。

如何正确地使用Python的属性和描述符

关于@property装饰器

在Python中我们使用@property装饰器来把对函数的调用伪装成对属性的访问。

那么为什么要这样做呢?因为@property让我们将自定义的代码同变量的访问/设定联系在了一起,同时为你的类保持一个简单的访问属性的接口。

举个栗子,假如我们有一个需要表示电影的类:

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

你开始在项目的其他地方使用这个类,但是之后你意识到:如果不小心给电影打了负分怎么办?你觉得这是错误的行为,希望Movie类可以阻止这个错误。 你首先想到的办法是将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

但这行不通。因为其他部分的代码都是直接通过Movie.score来赋值的。这个新修改的类只会在__init__方法中捕获错误的数据,但对于已经存在的类实例就无能为力了。如果有人试着运行m.scrore= -100,那么谁也没法阻止。那该怎么办?

Python的property解决了这个问题。

我们可以这样做

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都会检测它是否小于0。

property的不足

对property来说,最大的缺点就是它们不能重复使用。举个例子,假设你想为ticket字段也添加非负检查。下面是修改过的新类:

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")

可以看到代码增加了不少,但重复的逻辑也出现了不少。虽然property可以让类从外部看起来接口整洁漂亮,但是却做不到内部同样整洁漂亮。

描述符登场

什么是描述符?

一般来说,描述符是一个具有绑定行为的对象属性,其属性的访问被描述符协议方法覆写。这些方法是__get__()、__set__()和__delete__(),一个对象中只要包含了这三个方法中的至少一个就称它为描述符。

描述符有什么作用?

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.—–摘自官方文档

简单的说描述符会改变一个属性的基本的获取、设置和删除方式。

先看如何用描述符来解决上面 property逻辑重复的问题。

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')

因为描述符优先级高并且会改变默认的get、set行为,这样一来,当我们访问或者设置Movie().score的时候都会受到描述符Integer的限制。

不过我们也总不能用下面这样的方式来创建实例。

a = Movie()

a.score = 1

a.ticket = 2

a.title = ‘test’

a.descript = ‘…’

这样太生硬了,所以我们还缺一个构造函数。

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

这样在获取、设置和删除score和ticket的时候都会进入Integer的__get__、__set__,从而减少了重复的逻辑。

现在虽然问题得到了解决,但是你可能会好奇这个描述符到底是如何工作的。具体来说,在__init__函数里访问的是自己的self.score和self.ticket,怎么和类属性score和ticket关联起来的?

描述符如何工作

看官方的说明

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.

类调用__getattribute__()的时候大概是下面这样子:

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

下面是摘自国外一篇博客上的内容。

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__.

我对上面的理解是,访问一个实例的属性的时候是先遍历它和它的父类,寻找它们的__dict__里是否有同名的data descriptor如果有,就用这个data descriptor代理该属性,如果没有再寻找该实例自身的__dict__,如果有就返回。任然没有再查找它和它父类里的non-data descriptor,最后查找是否有__getattr__

描述符的应用场景

python的property、classmethod修饰器本身也是一个描述符,甚至普通的函数也是描述符(non-data discriptor)

django model和SQLAlchemy里也有描述符的应用

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

后记

只有当确实需要在访问属性的时候完成一些额外的处理任务时,才应该使用property。不然代码反而会变得更加啰嗦,而且这样会让程序变慢很多。

python 关于函数的语法

这里的QuickSort.count叫做"函数属性function attribute",

python等动态类型语言所具有的"函数同时是头等对象"的功能.

即代码可以往函数对象上灵活地添加某属性。

def f():   

print(f.act)

f.act=123 #定义和添加一个函数对象的属性-函数属性

f() #打印123

之前的快速排序用了一个count属性在记录排序算法的比较次数。属于调试显示,不是排序的核心算法..

二级Python----Python的内置函数及标准库(DAY 8)

python的内置函数(68个)

Python考核31个内置函数,

python内置了很多内置函数、类方法属性及各种模块。当我们想要当我们想要了解某种类型有哪些属性方法以及每种方法该怎么使用时,我们可以使用dir()函数和help()函数在python idle交互式模式下获得我们想要的信息。

• dir()函数获得对象中可用属性的列表

Python中的关键词有哪些?

dir(__builtins__):查看python内置函数

help(‘keywords‘):查看python关键词

如微分积分方程的求解程序、访问互联网、获取日期和时间、机器学习算法等。这些程序往往被收入程序库中,构成程序库。

只有经过严格检验的程序才能放在程序库里。检验,就是对程序作充分的测试。通常进行的有正确性测试、精度测试、速度测试、边界条件和出错状态的测试。经过检验的程序不但能保证计算结果的正确性,而且对错误调用也能作出反应。程序库中的程序都是规范化的。所谓规范化有三重含义:①同一库里所有程序的格式是统一的;② 对这些程序的调用方法是相同的;③ 每个程序所需参数的数目、顺序和类型都是严格规定好的。

Python的库包含标准库和第三方库

标准库:程序语言自身拥有的库,可以直接使用。help('modules')

第三方库:第三方者使用该语言提供的程序库。

标准库: turtle 库(必选)、 random 库(必选)、 time 库(可选)。

• turtle 库:图形绘制库

原理如同控制一只海龟,以不同的方向和速度进行位移而得到其运动轨迹。

使用模块的帮助时,需要先将模块导入。

例如:在IDLE中输入import turtle

dir(turtle)

help(turtle.**)

1.画布

画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置。

setup()方法用于初始化画布窗口大小和位置,参数包括画布窗口宽、画布窗口高、窗口在屏幕的水平起始位置和窗口在屏幕的垂直起始位置。

参数:width, height: 输入宽和高为整数时,表示 像素 ;为小数时,表示占据电脑屏幕的比例。(startx,starty):这一坐标表示

矩形窗口左上角顶点的位置,如果为空,则窗口位于屏幕中心:

例如:setup(640,480,300,300)表示在桌面屏幕(300,300)位置开始创建640×480大小的画布窗体。

2、画笔

• color() 用于设置或返回画笔颜色和填充颜色。

例如:color(‘red’)将颜色设为红色,也可用fillcolor()方法设置或返回填充颜色,或用pencolor()方法设置或返回笔触颜色。

python的内置函数有哪些,都是什么意思?

print-输出,input-输入,int-将字符串转数字(字符串必须是数字),str-将数字转为字符串,list-将字符串/数字转为列表,for-有限循环,while-无限循环……………………………………

分享标题:mysql解压版本怎么用,mysql57解压版安装教程
当前地址:https://www.cdcxhl.com/article42/hcgoec.html

成都网站建设公司_创新互联,为您提供Google企业建站网站改版小程序开发外贸网站建设商城网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站