继续学习Python

今天学习Python的内容是:
从函数到高级魔法方法,关键是高级魔方方法这部分
函数
函数的定义
函数的调用
函数文档
函数参数
函数的返回值
变量作用域
Lambda 表达式
匿名函数的定义
匿名函数的应用
类与对象
对象 = 属性 + 方法
self 是什么?
Python 的魔法方法
公有和私有
继承
组合
类、类对象和实例对象
什么是绑定?
一些相关的内置函数(BIF)
魔法方法
基本的魔法方法
算术运算符
反算术运算符
增量赋值运算符
一元运算符
属性访问
描述符
定制序列
迭代器

以下是魔方的学习笔记
魔法方法
魔法方法总是被双下划线包围,例如__init__。

魔法方法是面向对象的 Python 的一切,如果你不知道魔法方法,说明你还没能意识到面向对象的 Python 的强大。

魔法方法的“魔力”体现在它们总能够在适当的时候被自动调用。

魔法方法的第一个参数应为cls(类方法) 或者self(实例方法)。

cls:代表一个类的名称
self:代表一个实例对象的名称
基本的魔法方法
init(self[, …]) 构造器,当一个实例被创建的时候调用的初始化方法
【例子】

class Rectangle:
def init(self, x, y):
self.x = x
self.y = y

def getPeri(self):
    return (self.x + self.y) * 2

def getArea(self):
    return self.x * self.y

rect = Rectangle(4, 5)
print(rect.getPeri()) # 18
print(rect.getArea()) # 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
new(cls[, …]) 在一个对象实例化的时候所调用的第一个方法,在调用__init__初始化前,先调用__new__。
new__至少要有一个参数cls,代表要实例化的类,此参数在实例化时由 Python 解释器自动提供,后面的参数直接传递给__init
new__对当前类进行了实例化,并将实例返回,传给__init__的self。但是,执行了__new,并不一定会进入__init__,只有__new__返回了,当前类cls的实例,当前类的__init__才会进入。
【例子】

class A(object):
def init(self, value):
print(“into A init”)
self.value = value

def __new__(cls, *args, **kwargs):
    print("into A __new__")
    print(cls)
    return object.__new__(cls)

class B(A):
def init(self, value):
print(“into B init”)
self.value = value

def __new__(cls, *args, **kwargs):
    print("into B __new__")
    print(cls)
    return super().__new__(cls, *args, **kwargs)

b = B(10)

结果:

into B new

<class ‘main.B’>

into A new

<class ‘main.B’>

into B init

class A(object):
def init(self, value):
print(“into A init”)
self.value = value

def __new__(cls, *args, **kwargs):
    print("into A __new__")
    print(cls)
    return object.__new__(cls)

class B(A):
def init(self, value):
print(“into B init”)
self.value = value

def __new__(cls, *args, **kwargs):
    print("into B __new__")
    print(cls)
    return super().__new__(A, *args, **kwargs)  # 改动了cls变为A

b = B(10)

结果:

into B new

<class ‘main.B’>

into A new

<class ‘main.A’>

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
若__new__没有正确返回当前类cls的实例,那__init__是不会被调用的,即使是父类的实例也不行,将没有__init__被调用。
【例子】利用__new__实现单例模式。

class Earth:
pass

a = Earth()
print(id(a)) # 260728291456
b = Earth()
print(id(b)) # 260728291624

class Earth:
__instance = None # 定义一个类属性做判断

def __new__(cls):
    if cls.__instance is None:
        cls.__instance = object.__new__(cls)
        return cls.__instance
    else:
        return cls.__instance

a = Earth()
print(id(a)) # 512320401648
b = Earth()
print(id(b)) # 512320401648
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
__new__方法主要是当你继承一些不可变的 class 时(比如int, str, tuple), 提供给你一个自定义这些类的实例化过程的途径。
【例子】

class CapStr(str):
def new(cls, string):
string = string.upper()
return str.new(cls, string)

a = CapStr(“i love lsgogroup”)
print(a) # I LOVE LSGOGROUP
1
2
3
4
5
6
7
8
del(self) 析构器,当一个对象将要被系统回收之时调用的方法。
Python 采用自动引用计数(ARC)方式来回收对象所占用的空间,当程序中有一个变量引用该 Python 对象时,Python会自动保证该对象引用计数为 1;当程序中有两个变量引用该 Python 对象时,Python 会自动保证该对象引用计数为2,依此类推,如果一个对象的引用计数变成了 0,则说明程序中不再有变量引用该对象,表明程序不再需要该对象,因此 Python就会回收该对象。

大部分时候,Python 的 ARC 都能准确、高效地回收系统中的每个对象。但如果系统中出现循环引用的情况,比如对象 a 持有一个实例变量引用对象 b,而对象 b 又持有一个实例变量引用对象 a,此时两个对象的引用计数都是 1,而实际上程序已经不再有变量引用它们,系统应该回收它们,此时 Python的垃圾回收器就可能没那么快,要等专门的循环垃圾回收器(Cyclic Garbage Collector)来检测并回收这种引用循环。

【例子】

class C(object):
def init(self):
print(‘into C init’)

def __del__(self):
    print('into C __del__')

c1 = C()

into C init

c2 = c1
c3 = c2
del c3
del c2
del c1

into C del

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
str(self):

当你打印一个对象的时候,触发__str__
当你使用%s格式化的时候,触发__str__
str强转数据类型的时候,触发__str__
repr(self):

repr是str的备胎
有__str__的时候执行__str__,没有实现__str__的时候,执行__repr__
repr(obj)内置函数对应的结果是__repr__的返回值
当你使用%r格式化的时候 触发__repr__
【例子】

class Cat:
“”“定义一个猫类”""

def __init__(self, new_name, new_age):
    """在创建完对象之后 会自动调用, 它完成对象的初始化的功能"""
    self.name = new_name
    self.age = new_age

def __str__(self):
    """返回一个对象的描述信息"""
    return "名字是:%s , 年龄是:%d" % (self.name, self.age)
    
def __repr__(self):
    """返回一个对象的描述信息"""
    return "Cat:(%s,%d)" % (self.name, self.age)

def eat(self):
    print("%s在吃鱼...." % self.name)

def drink(self):
    print("%s在喝可乐..." % self.name)

def introduce(self):
    print("名字是:%s, 年龄是:%d" % (self.name, self.age))

创建了一个对象

tom = Cat(“汤姆”, 30)
print(tom) # 名字是:汤姆 , 年龄是:30
print(str(tom)) # 名字是:汤姆 , 年龄是:30
print(repr(tom)) # Cat:(汤姆,30)
tom.eat() # 汤姆在吃鱼…
tom.introduce() # 名字是:汤姆, 年龄是:30
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
str(self) 的返回结果可读性强。也就是说,str 的意义是得到便于人们阅读的信息,就像下面的 ‘2019-10-11’ 一样。

repr(self) 的返回结果应更准确。怎么说,repr 存在的目的在于调试,便于开发者使用。

【例子】

import datetime

today = datetime.date.today()
print(str(today)) # 2019-10-11
print(repr(today)) # datetime.date(2019, 10, 11)
print(’%s’ %today) # 2019-10-11
print(’%r’ %today) # datetime.date(2019, 10, 11)
1
2
3
4
5
6
7
算数运算符
类型工厂函数,指的是“不通过类而是通过函数来创建对象”。

【例子】

class C:
pass

print(type(len)) # <class ‘builtin_function_or_method’>
print(type(dir)) # <class ‘builtin_function_or_method’>
print(type(int)) # <class ‘type’>
print(type(list)) # <class ‘type’>
print(type(tuple)) # <class ‘type’>
print(type©) # <class ‘type’>
print(int(‘123’)) # 123

这个例子中list工厂函数把一个元祖对象加工成了一个列表对象。

print(list((1, 2, 3))) # [1, 2, 3]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
add(self, other)定义加法的行为:+
sub(self, other)定义减法的行为:-
【例子】

class MyClass:

def __init__(self, height, weight):
    self.height = height
    self.weight = weight

# 两个对象的长相加,宽不变.返回一个新的类
def __add__(self, others):
    return MyClass(self.height + others.height, self.weight + others.weight)

# 两个对象的宽相减,长不变.返回一个新的类
def __sub__(self, others):
    return MyClass(self.height - others.height, self.weight - others.weight)

# 说一下自己的参数
def intro(self):
    print("高为", self.height, " 重为", self.weight)

def main():
a = MyClass(height=10, weight=5)
a.intro()

b = MyClass(height=20, weight=10)
b.intro()

c = b - a
c.intro()

d = a + b
d.intro()

if name == ‘main’:
main()

高为 10 重为 5

高为 20 重为 10

高为 10 重为 5

高为 30 重为 15

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
38
39
40
mul(self, other)定义乘法的行为:*
truediv(self, other)定义真除法的行为:/
floordiv(self, other)定义整数除法的行为://
mod(self, other) 定义取模算法的行为:%
divmod(self, other)定义当被 divmod() 调用时的行为
divmod(a, b)把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)。
【例子】

print(divmod(7, 2)) # (3, 1)
print(divmod(8, 2)) # (4, 0)
1
2
pow(self, other[, module])定义当被 power() 调用或 ** 运算时的行为
lshift(self, other)定义按位左移位的行为:<<
rshift(self, other)定义按位右移位的行为:>>
and(self, other)定义按位与操作的行为:&
xor(self, other)定义按位异或操作的行为:^
or(self, other)定义按位或操作的行为:|
反算术运算符
反运算魔方方法,与算术运算符保持一一对应,不同之处就是反运算的魔法方法多了一个“r”。当文件左操作不支持相应的操作时被调用。

radd(self, other)定义加法的行为:+
rsub(self, other)定义减法的行为:-
rmul(self, other)定义乘法的行为:*
rtruediv(self, other)定义真除法的行为:/
rfloordiv(self, other)定义整数除法的行为://
rmod(self, other) 定义取模算法的行为:%
rdivmod(self, other)定义当被 divmod() 调用时的行为
rpow(self, other[, module])定义当被 power() 调用或 ** 运算时的行为
rlshift(self, other)定义按位左移位的行为:<<
rrshift(self, other)定义按位右移位的行为:>>
rand(self, other)定义按位与操作的行为:&
rxor(self, other)定义按位异或操作的行为:^
ror(self, other)定义按位或操作的行为:|
a + b
1
这里加数是a,被加数是b,因此是a主动,反运算就是如果a对象的__add__()方法没有实现或者不支持相应的操作,那么 Python 就会调用b的__radd__()方法。

【例子】

class Nint(int):
def radd(self, other):
return int.sub(other, self) # 注意 self 在后面

a = Nint(5)
b = Nint(3)
print(a + b) # 8
print(1 + b) # -2
1
2
3
4
5
6
7
8
9
增量赋值运算符
iadd(self, other)定义赋值加法的行为:+=
isub(self, other)定义赋值减法的行为:-=
imul(self, other)定义赋值乘法的行为:*=
itruediv(self, other)定义赋值真除法的行为:/=
ifloordiv(self, other)定义赋值整数除法的行为://=
imod(self, other)定义赋值取模算法的行为:%=
ipow(self, other[, modulo])定义赋值幂运算的行为:**=
ilshift(self, other)定义赋值按位左移位的行为:<<=
irshift(self, other)定义赋值按位右移位的行为:>>=
iand(self, other)定义赋值按位与操作的行为:&=
ixor(self, other)定义赋值按位异或操作的行为:^=
ior(self, other)定义赋值按位或操作的行为:|=
一元运算符
neg(self)定义正号的行为:+x
pos(self)定义负号的行为:-x
abs(self)定义当被abs()调用时的行为
invert(self)定义按位求反的行为:~x
属性访问
getattr(self, name): 定义当用户试图获取一个不存在的属性时的行为。
getattribute(self, name):定义当该类的属性被访问时的行为(先调用该方法,查看是否存在该属性,若不存在,接着去调用__getattr__)。
setattr(self, name, value):定义当一个属性被设置时的行为。
delattr(self, name):定义当一个属性被删除时的行为。
【例子】

class C:
def getattribute(self, item):
print(‘getattribute’)
return super().getattribute(item)

def __getattr__(self, item):
    print('__getattr__')

def __setattr__(self, key, value):
    print('__setattr__')
    super().__setattr__(key, value)

def __delattr__(self, item):
    print('__delattr__')
    super().__delattr__(item)

c = C()
c.x

getattribute

getattr

c.x = 1

setattr

del c.x

delattr

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
描述符
描述符就是将某种特殊类型的类的实例指派给另一个类的属性。

get(self, instance, owner)用于访问属性,它返回属性的值。
set(self, instance, value)将在属性分配操作中调用,不返回任何内容。
del(self, instance)控制删除操作,不返回任何内容。
【例子】

class MyDecriptor:
def get(self, instance, owner):
print(‘get’, self, instance, owner)

def __set__(self, instance, value):
    print('__set__', self, instance, value)

def __delete__(self, instance):
    print('__delete__', self, instance)

class Test:
x = MyDecriptor()

t = Test()
t.x

get <main.MyDecriptor object at 0x000000CEAAEB6B00> <main.Test object at 0x000000CEABDC0898> <class ‘main.Test’>

t.x = ‘x-man’

set <main.MyDecriptor object at 0x00000023687C6B00> <main.Test object at 0x00000023696B0940> x-man

del t.x

delete <main.MyDecriptor object at 0x000000EC9B160A90> <main.Test object at 0x000000EC9B160B38>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
定制序列
协议(Protocols)与其它编程语言中的接口很相似,它规定你哪些方法必须要定义。然而,在 Python 中的协议就显得不那么正式。事实上,在 Python 中,协议更像是一种指南。

容器类型的协议

如果说你希望定制的容器是不可变的话,你只需要定义__len__()和__getitem__()方法。
如果你希望定制的容器是可变的话,除了__len__()和__getitem__()方法,你还需要定义__setitem__()和__delitem__()两个方法。
【例子】编写一个不可改变的自定义列表,要求记录列表中每个元素被访问的次数。

class CountList:
def init(self, *args):
self.values = [x for x in args]
self.count = {}.fromkeys(range(len(self.values)), 0)

def __len__(self):
    return len(self.values)

def __getitem__(self, item):
    self.count[item] += 1
    return self.values[item]

c1 = CountList(1, 3, 5, 7, 9)
c2 = CountList(2, 4, 6, 8, 10)
print(c1[1]) # 3
print(c2[2]) # 6
print(c1[1] + c2[1]) # 7

print(c1.count)

{0: 0, 1: 2, 2: 0, 3: 0, 4: 0}

print(c2.count)

{0: 0, 1: 1, 2: 1, 3: 0, 4: 0}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
len(self)定义当被len()调用时的行为(返回容器中元素的个数)。
getitem(self, key)定义获取容器中元素的行为,相当于self[key]。
setitem(self, key, value)定义设置容器中指定元素的行为,相当于self[key] = value。
delitem(self, key)定义删除容器中指定元素的行为,相当于del self[key]。
【例子】编写一个可改变的自定义列表,要求记录列表中每个元素被访问的次数。

class CountList:
def init(self, *args):
self.values = [x for x in args]
self.count = {}.fromkeys(range(len(self.values)), 0)

def __len__(self):
    return len(self.values)

def __getitem__(self, item):
    self.count[item] += 1
    return self.values[item]

def __setitem__(self, key, value):
    self.values[key] = value

def __delitem__(self, key):
    del self.values[key]
    for i in range(0, len(self.values)):
        if i >= key:
            self.count[i] = self.count[i + 1]
    self.count.pop(len(self.values))

c1 = CountList(1, 3, 5, 7, 9)
c2 = CountList(2, 4, 6, 8, 10)
print(c1[1]) # 3
print(c2[2]) # 6
c2[2] = 12
print(c1[1] + c2[2]) # 15
print(c1.count)

{0: 0, 1: 2, 2: 0, 3: 0, 4: 0}

print(c2.count)

{0: 0, 1: 0, 2: 2, 3: 0, 4: 0}

del c1[1]
print(c1.count)

{0: 0, 1: 0, 2: 0, 3: 0}

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
迭代器
迭代是 Python 最强大的功能之一,是访问集合元素的一种方式。
迭代器是一个可以记住遍历的位置的对象。
迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。
迭代器只能往前不会后退。
字符串,列表或元组对象都可用于创建迭代器:
【例子】

string = ‘lsgogroup’
for c in string:
print©

‘’’
l
s
g
o
g
r
o
u
p
‘’’

for c in iter(string):
print©
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
links = {‘B’: ‘百度’, ‘A’: ‘阿里’, ‘T’: ‘腾讯’}
for each in links:
print(’%s -> %s’ % (each, links[each]))

‘’’
B -> 百度
A -> 阿里
T -> 腾讯
‘’’

for each in iter(links):
print(’%s -> %s’ % (each, links[each]))
1
2
3
4
5
6
7
8
9
10
11
12
迭代器有两个基本的方法:iter() 和 next()。
iter(object) 函数用来生成迭代器。
next(iterator[, default]) 返回迭代器的下一个项目。
iterator – 可迭代对象
default – 可选,用于设置在没有下一个元素时返回该默认值,如果不设置,又没有下一个元素则会触发 StopIteration 异常。
【例子】

links = {‘B’: ‘百度’, ‘A’: ‘阿里’, ‘T’: ‘腾讯’}

it = iter(links)
while True:
try:
each = next(it)
except StopIteration:
break
print(each)

B

A

T

it = iter(links)
print(next(it)) # B
print(next(it)) # A
print(next(it)) # T
print(next(it)) # StopIteration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
把一个类作为一个迭代器使用需要在类中实现两个魔法方法 iter() 与 next() 。

iter(self)定义当迭代容器中的元素的行为,返回一个特殊的迭代器对象, 这个迭代器对象实现了 next() 方法并通过 StopIteration 异常标识迭代的完成。
next() 返回下一个迭代器对象。
StopIteration 异常用于标识迭代的完成,防止出现无限循环的情况,在 next() 方法中我们可以设置在完成指定循环次数后触发 StopIteration 异常来结束迭代。
【例子】

lass Fibs:
def init(self, n=10):
self.a = 0
self.b = 1
self.n = n

def __iter__(self):
    return self

def __next__(self):
    self.a, self.b = self.b, self.a + self.b
    if self.a > self.n:
        raise StopIteration
    return self.a

fibs = Fibs(100)
for each in fibs:
print(each, end=’ ')

1 1 2 3 5 8 13 21 34 55 89

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
生成器

在 Python 中,使用了 yield 的函数被称为生成器(generator)。
跟普通函数不同的是,生成器是一个返回迭代器的函数,只能用于迭代操作,更简单点理解生成器就是一个迭代器。
在调用生成器运行的过程中,每次遇到 yield 时函数会暂停并保存当前所有的运行信息,返回 yield 的值, 并在下一次执行 next() 方法时从当前位置继续运行。
调用一个生成器函数,返回的是一个迭代器对象。
【例子】

def myGen():
print(‘生成器执行!’)
yield 1
yield 2

myG = myGen()
for each in myG:
print(each)

‘’’
生成器执行!
1
2
‘’’

myG = myGen()
print(next(myG))

生成器执行!

1

print(next(myG)) # 2
print(next(myG)) # StopIteration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
【例子】用生成器实现斐波那契数列。

def libs(n):
a = 0
b = 1
while True:
a, b = b, a + b
if a > n:
return
yield a

for each in libs(100):
print(each, end=’ ')

1 1 2 3 5 8 13

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值