【Python基础 ---- Task 3】

天池龙珠计划Python训练营


前言

Python 是一种通用编程语言:

其在科学计算和机器学习领域具有广泛的应用。


一、函数

1,函数的定义

python 万物皆可函数,函数之间可以相互调用(可以从另一个函数中返回出来而去构建高阶函数)

def function1(参数]【根据自己想要的函数参数来定】)return hello
	
def function2():
	expression = function1() 
	return [expression]
	

2,函数的调用

if __name__ == "__main__":
	result = function1()   # 我要调用用户自定义函数!
	print(result) --> hello
	result = function2()   # 我要调用用户自定义函数!
	print(result) --> hello
3,函数文档
def function1(name):# name = 行参
	result = name
	print(result)

if __name__ == "__main__":
	function1(码农yyds)  #  码农 = 实参
4. 函数参数

Python 的函数具有非常灵活多样的参数形态,既可以实现简单的调用,又可以传入非常复杂的参数。从简到繁的参数形

def function1(arg1 , arg2 = name, *args, **kwargs):
	arg1 == 位置参数 (positional argument)
	arg2 == 默认参数 (default argument)
	*args == 可变参数 (variable argument)
	**kwargs  == 关键字参数 (keyword argument)

	-------------------------------------------

命名关键字参数 --  指定参数
def functionname(arg1, arg2=v, args, *, nkw, *kw):
       "函数文档字符串"
       functionsuite
       return [expression]
def printinfo(arg1, *, nkw, **kwargs):
    print(arg1)  # 70
    print(nkw)   # 10
    print(kwargs)# {'a': 1, 'b': 2}
printinfo(70, nkw=10, a=1, b=2)
printinfo(70, 10, a=1, b=2)  # 位置参数表意不明
# TypeError: printinfo() takes 1 positional argument but 2 were given
    --------------------------------------------

参数组合
位置参数、默认参数、可变参数和关键字参数。
位置参数、默认参数、命名关键字参数和关键字参数。
*args 是可变参数,args 接收的是一个 tuple
**kw 是关键字参数,kw 接收的是一个 dict
命名关键字参数是为了限制调用者可以传入的参数名,同时可以提供默认值。定义命名关键字参数不要忘了写分隔符 *,否则定义的是位置参数。

警告:虽然可以组合多达 5 种参数,但不要同时使用太多的组合,否则函数很难懂

5,函数的返回值
def function1(参数]【根据自己想要的函数参数来定】)return hello  #  函数的返回值
6, 变量作用域

可以理解为局部和全局
局部是函数内部可以调用,外部不行
全局就是个个函数都可以调用

number = 12
def func_first():
	result = number  # 可以获取全局的number  === 全局
	number2 = 13
	print(result)  #values_true  12
	
def func_sec():
	result = number2 # 获取不到上一个函数里面的参数 === 局部
	print(result)  # values_False  什么都没有

局部变量只能在其被声明的函数内部访问,而全局变量可以在整个程序范围内访问。

当内部作用域想修改外部作用域的变量时,就要用到global和nonlocal关键字

number = 12
def func_first():
	global = number
	number=123  # 可以获取全局的number  === 全局
	print(result)  #values_true  123

内嵌函数

def outer():
    print('outer函数在这被调用')
    def inner():
        print('inner函数在这被调用')
    inner()  # 该函数只能在outer函数内部被调用

outer()
# outer函数在这被调用
# inner函数在这被调用

闭包

是函数式编程的一个重要的语法结构,是一种特殊的内嵌函数。
如果在一个内部函数里对外层非全局作用域的变量进行引用,那么内部函数就被认为是闭包。
通过闭包可以访问外层非全局作用域的变量,这个作用域称为 闭包作用域。

def funX(x):
    def funY(y):
        return x * y
    def funZ(z):
    	return z
    return funY
    
i = funX(8)
print(i(5))  # 40

闭包的返回值通常是函数。
print(type(i))  # <class 'function'>
funY,funZ = funX(8)  两个函数

如果要修改闭包作用域中的变量则需要 nonlocal 关键字

def outer():
    num = 10

    def inner():
        nonlocal num  # nonlocal关键字声明
        num = 100
        print(num)

    inner()
    print(num)


outer()

# 100
# 100

gloal 在 nonlocal 的上一层

递归

如果一个函数在内部调用自身本身,这个函数就是递归函数

n! = 1 x 2 x 3 x ... x n
# 利用循环
n = 5
for k in range(1, 5):
    n = n * k
print(n)  # 120

# 利用递归
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n - 1)

print(factorial(5)) # 120

斐波那契数列

f(n)=f(n-1)+f(n-2), f(0)=0 f(1)=1
 利用循环
i = 0
j = 1
lst = list([i, j])
for k in range(2, 11):
    k = i + j
    lst.append(k)
    i = j
    j = k
print(lst)  
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

# 利用递归
def recur_fibo(n):
    if n <= 1:
        return n
    return recur_fibo(n - 1) + recur_fibo(n - 2)


lst = list()
for k in range(11):
    lst.append(recur_fibo(k))
print(lst)  
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

递归详解,百度url — 秒懂

设置递归的层数,Python默认递归层数为 100
import sys
sys.setrecursionlimit(1000)

二、Lambda-表达式

1. 匿名函数的定义

Python 里有两类函数:

  • 第一类:用 def 关键词定义的正规函数
  • 第二类:用 lambda 关键词定义的匿名函数
    Python 使用 lambda 关键词来创建匿名函数,而非def关键词,它没有函数名,其语法结构如下:

lambda argument_list: expression

  • lambda - 定义匿名函数的关键词。
  • argument_list - 函数参数,它们可以是位置参数、默认参数、关键字参数,和正规函数里的参数类型一样。
  • :- 冒号,在函数参数和表达式中间要加个冒号。
  • expression - 只是一个表达式,输入函数参数,输出一些值。

注意:

  • expression 中没有 return 语句,因为 lambda 不需要它来返回,表达式本身结果就是返回值。
  • 匿名函数拥有自己的命名空间,且不能访问自己参数列表之外或全局命名空间里的参数。
def sqr(x):
    return x ** 2


print(sqr)
# <function sqr at 0x000000BABD3A4400>

y = [sqr(x) for x in range(10)]
print(y)
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

lbd_sqr = lambda x: x ** 2
print(lbd_sqr)
# <function <lambda> at 0x000000BABB6AC1E0>

y = [lbd_sqr(x) for x in range(10)]
print(y)
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]


sumary = lambda arg1, arg2: arg1 + arg2
print(sumary(10, 20))  # 30

func = lambda *args: sum(args)
print(func(1, 2, 3, 4, 5))  # 15
2. 匿名函数的应用

函数式编程 是指代码中每一块都是不可变的,都由纯函数的形式组成。这里的纯函数,是指函数本身相互独立、互不影响,对于相同的输入,总会有相同的输出,没有任何副作用。 可以理解为原来的元素复制放到了一个ip地址中,并没有改变原来的元素。
非函数式编程 是指对原始数据直接进行元素的替换。

匿名函数 常常应用于函数式编程的高阶函数 (high-order function)中,主要有两种形式:

  • 参数是函数 (filter, map)
  • 返回值是函数 (closure)

如,在 filter和map函数中的应用:

  • filter(function, iterable) 过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 list() 来转换。
  • map(function, *iterables) 根据提供的函数对指定序列做映射。
odd = lambda x: x % 2 == 1
templist = filter(odd, [1, 2, 3, 4, 5, 6, 7, 8, 9])
print(list(templist))  # [1, 3, 5, 7, 9]

m1 = map(lambda x: x ** 2, [1, 2, 3, 4, 5])
print(list(m1))  
# [1, 4, 9, 16, 25]

m2 = map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
print(list(m2))  
# [3, 7, 11, 15, 19]

高阶函数

def apply_to_list(fun, some_list):
    return fun(some_list)

lst = [1, 2, 3, 4, 5]
print(apply_to_list(sum, lst))
# 15

print(apply_to_list(len, lst))
# 5

print(apply_to_list(lambda x: sum(x) / len(x), lst))
# 3.0

三、类与对象

1. 属性和方法组成对象
class ClaccTion (objeck):
	# 属性
	color = red
	legs = 4
	# 方法
	def function(self):
		pass

封装:信息隐蔽技术
继承:子类自动共享父类之间数据和方法的机制
多态:不同对象对同一方法响应不同的行动

2. self是什么?

Python 的 self 相当于 C++ 的 this 指针
类的方法与普通的函数只有一个特别的区别 —— 它们必须有一个额外的第一个参数名称(对应于该实例,即该对象本身),按照惯例它的名称是 self。在调用方法时,我们无需明确提供与参数 self 相对应的参数。

class Ball:
    def setName(self, name):
        self.name = name

    def kick(self):
        print("我叫%s," % self.name)
3. Python的魔法方法

1,一个名为**init(**self[, param1, param2…])的魔法方法,该方法在类实例化时会自动调用。

class Ball:
    def __init__(self, name):
        self.name = name

    def kick(self):
        print("我叫%s," % self.name)

2 ,new

init()方法很容易被认为是在实例化对象时调用的第一个方法,其实不是,当我们实例化一个对象的时候,例如class1=MyClass(1, 2),第一个被调用的是__new__()方法,而后再调用__init__()方法,并把一些参数传给__init__()方法,new()方法才是真正地创建了实例化对象,init()方法只是给这个对象进行了初始化操作。

class MyClass:
    def __init__(self, a, b):
        print("__init__()方法被调用")
        self.a = a
        self.b = b

    def __new__(cls, *args, **kwargs):
        print("__new__()方法被调用")
        print("args:", args)
        c = super().__new__(cls)
        return c


class1 = MyClass(1, 2)

del
    当一个对象的生命周期结束被彻底销毁的时候被调,__del__方法会被调用,不常用,一般不需要进行重写。
	若__new__没有正确返回当前类cls的实例,那__init__是不会被调用的,即使是父类的实例也不行,将没有__init__被调用。
	__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

3,str & repr
str()方法定义当被print()方法或者str()方法调用时的行为,repr()方法定义当被print()方法或者repr()方法调用时的行为,一般来说,它们的功能都是实现类到字符串的转化,实现方式上也没有特别的差异。 但实际上,str()方法的返回结果可读性应该要更强一些,repr()方法的返回结果要更加准确,也就是说,str()方法的意义是得到便于人们阅读的信息,而__repr__()方法的目的在于开发者便于调试。



__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
注意:当进行print()的时候,首先被执行的是__str__()方法,如果没定义__str__()方法,repr()方法才会被执行。

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

4,call
call()方法可以让类的实例像函数一样被调用

class Multiply:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __call__(self, *args, **kwargs):
        return self.x * self.y

p = Multiply(2, 3)
print(p())
当调用p()时,call()方法会被调用,返回参数x和y的乘积,call()方法也可以接受参数。

5,iter

如果我们希望一个实例化对象是一个迭代器,可被用于for…in循环遍历,这时我们需要在类中定义__iter__()方法和__next__()方法,iter()方法返回一个迭代对象,next()返回迭代器的下一个元素,这个在之前的文章有说过:迭代器Iterator的实现,这里就不展开了。
————————————————

百度方法乱解魔法

4. 公有和私有

在 Python 中定义私有变量只需要在变量名或函数名前加上“__”两个下划线,那么这个函数或变量就会为私有的了。

class JustCounter:
    __secretCount = 0  # 私有变量
    publicCount = 0  # 公开变量

    def count(self):
        self.__secretCount += 1
        self.publicCount += 1
        print(self.__secretCount)

    def __foo(self):  # 私有方法
        print('这是私有方法')

    def foo(self):  # 公共方法
        print('这是公共方法')
        self.__foo()

counter = JustCounter()
counter.count()  # 1
counter.count()  # 2
print(counter.publicCount)  # 2

# Python的私有为伪私有
print(counter._JustCounter__secretCount)  # 2 
print(counter.__secretCount)  
# AttributeError: 'JustCounter' object has no attribute '__secretCount'

counter.foo()
# 这是公共方法
# 这是私有方法

counter.__foo()
# AttributeError: 'Site' object has no attribute '__foo'

注意: 带有__私有属性,方法只能在类内之间调用  在外不可调用
5. 继承

Python 同样支持类的继承,派生类
class DerivedClassName(BaseClassName)

BaseClassName(基类名)必须与派生类定义在一个作用域内。除了类,还可以用表达式,基类定义在另一个模块中时这一点非常有用
class DerivedClassName(modname.BaseClassName):

如果子类中定义与父类同名的方法或属性,则会自动覆盖父类对应的方法或属性。

# 类定义
class people:
    # 定义基本属性
    name = ''
    age = 0
    # 定义私有属性,私有属性在类外部无法直接进行访问
    __weight = 0

    # 定义构造方法
    def __init__(self, n, a, w):
        self.name = n
        self.age = a
        self.__weight = w

    def speak(self):
        print("%s 说: 我 %d 岁。" % (self.name, self.age))


# 单继承示例
class student(people):
    grade = ''

    def __init__(self, n, a, w, g):
        # 调用父类的构函
        people.__init__(self, n, a, w)
        self.grade = g

    # 覆写父类的方法
    def speak(self):
        print("%s 说: 我 %d 岁了,我在读 %d 年级" % (self.name, self.age, self.grade))


s = student('小马的程序人生', 10, 60, 3)
s.speak()
# 小马的程序人生 说:10 岁了,我在读 3 年级

注意:如果上面的程序去掉:people.__init__(self, n, a, w),则输出:说:0 岁了,我在读 3 年级,因为子类的构造方法把父类的构造方法覆盖了。


问题报表:
people -- Fish  -- super
import random

class Fish:
    def __init__(self):
        self.x = random.randint(0, 10)
        self.y = random.randint(0, 10)

    def move(self):
        self.x -= 1
        print("我的位置", self.x, self.y)


class GoldFish(Fish):  # 金鱼
    pass


class Carp(Fish):  # 鲤鱼
    pass


class Salmon(Fish):  # 三文鱼
    pass


class Shark(Fish):  # 鲨鱼
    def __init__(self):
        self.hungry = True

    def eat(self):
        if self.hungry:
            print("吃货的梦想就是天天有得吃!")
            self.hungry = False
        else:
            print("太撑了,吃不下了!")
            self.hungry = True


g = GoldFish()
g.move()  # 我的位置 9 4
s = Shark()
s.eat() # 吃货的梦想就是天天有得吃!
s.move()  
# AttributeError: 'Shark' object has no attribute 'x'   ----- 解决问题

调用未绑定的父类方法Fish.__init__(self)
class Shark(Fish):  # 鲨鱼
    def __init__(self):
        Fish.__init__(self)
        self.hungry = True

    def eat(self):
        if self.hungry:
            print("吃货的梦想就是天天有得吃!")
            self.hungry = False
        else:
            print("太撑了,吃不下了!")
            self.hungry = True


使用super函数super().__init__()
class Shark(Fish):  # 鲨鱼
    def __init__(self):
        super().__init__()
        self.hungry = True

    def eat(self):
        if self.hungry:
            print("吃货的梦想就是天天有得吃!")
            self.hungry = False
        else:
            print("太撑了,吃不下了!")
            self.hungry = True


Python 虽然支持多继承的形式,但我们一般不使用多继承,因为容易引起混乱。
class DerivedClassName(Base1, Base2, Base3):

需要注意圆括号中父类的顺序,若是父类中有相同的方法名,而在子类使用时未指定,Python 从左至右搜索,即方法在子类中未找到时,从左到右查找父类中是否包含方法。

# 类定义
class People:
    # 定义基本属性
    name = ''
    age = 0
    # 定义私有属性,私有属性在类外部无法直接进行访问
    __weight = 0

    # 定义构造方法
    def __init__(self, n, a, w):
        self.name = n
        self.age = a
        self.__weight = w

    def speak(self):
        print("%s 说: 我 %d 岁。" % (self.name, self.age))


# 单继承示例
class Student(People):
    grade = ''

    def __init__(self, n, a, w, g):
        # 调用父类的构函
        People.__init__(self, n, a, w)
        self.grade = g

    # 覆写父类的方法
    def speak(self):
        print("%s 说: 我 %d 岁了,我在读 %d 年级" % (self.name, self.age, self.grade))


# 另一个类,多重继承之前的准备
class Speaker:
    topic = ''
    name = ''
    
    def __init__(self, n, t):
    self.name = n
    self.topic = t

    def speak(self):
        print("我叫 %s,我是一个演说家,我演讲的主题是 %s" % (self.name, self.topic))


# 多重继承
class Sample01(Speaker, Student):  # 从左到右优先输出
    a = ''

    def __init__(self, n, a, w, g, t):
        Student.__init__(self, n, a, w, g)
        Speaker.__init__(self, n, t)

# 方法名同,默认调用的是在括号中排前地父类的方法
test = Sample01("Tim", 25, 80, 4, "Python")
test.speak()  
# 我叫 Tim,我是一个演说家,我演讲的主题是 Python

class Sample02(Student, Speaker):  # 从左到右优先输出
    a = ''

    def __init__(self, n, a, w, g, t):
        Student.__init__(self, n, a, w, g)
        Speaker.__init__(self, n, t)

# 方法名同,默认调用的是在括号中排前地父类的方法
test = Sample02("Tim", 25, 80, 4, "Python")
test.speak()  
# Tim 说:25 岁了,我在读 4 年级
6. 组合
class Turtle:
    def __init__(self, x):
        self.num = x


class Fish:
    def __init__(self, x):
        self.num = x


class Pool:
    def __init__(self, x, y):
        self.turtle = Turtle(x)
        self.fish = Fish(y)

    def print_num(self):
        print("水池里面有乌龟%s只,小鱼%s条" % (self.turtle.num, self.fish.num))


p = Pool(2, 3)
p.print_num()
# 水池里面有乌龟2只,小鱼3
7. 类、类对象和实例对象

类对象:创建一个类,其实也是一个对象也在内存开辟了一块空间,称为类对象,类对象只有一个。
实例对象:就是通过实例化类创建的对象,称为实例对象,实例对象可以有多个。

class A(object):
    a = 0  #类属性
    def __init__(self, xx):
        A.a = xx  #使用类属性可以通过 (类名.类属性)调用。

# 实例化对象 a、b、c都属于实例对象。
a = A()
b = A()
c = A()

# 创建类对象
class Test(object):
    class_attr = 100  # 类属性

    def __init__(self):
        self.sl_attr = 100  # 实例属性

    def func(self):
        print('类对象.类属性的值:', Test.class_attr)  # 调用类属性
        print('self.类属性的值', self.class_attr)  # 相当于把类属性 变成实例属性
        print('self.实例属性的值', self.sl_attr)  # 调用实例属性


a = Test()
a.func()

# 类对象.类属性的值: 100
# self.类属性的值 100
# self.实例属性的值 100

b = Test()
b.func()

# 类对象.类属性的值: 100
# self.类属性的值 100
# self.实例属性的值 100

a.class_attr = 200
a.sl_attr = 200
a.func()

# 类对象.类属性的值: 100
# self.类属性的值 200
# self.实例属性的值 200

b.func()

# 类对象.类属性的值: 100
# self.类属性的值 100
# self.实例属性的值 100

Test.class_attr = 300
a.func()

# 类对象.类属性的值: 300
# self.类属性的值 200
# self.实例属性的值 200

b.func()
# 类对象.类属性的值: 300
# self.类属性的值 300
# self.实例属性的值 100

注意:属性与方法名相同,属性会覆盖方法。
8. 什么是绑定?

Python 严格要求方法需要有实例才能被调用,这种限制其实就是 Python 所谓的绑定概念。

Python 对象的数据属性通常存储在名为.__ dict__的字典中,我们可以直接访问__dict__,或利用 Python 的内置函数vars()获取.__ dict__。

class CC:
    def setXY(self, x, y):
        self.x = x
        self.y = y

    def printXY(self):
        print(self.x, self.y)


dd = CC()
print(dd.__dict__)
# {}

print(vars(dd))
# {}

print(CC.__dict__)
# {'__module__': '__main__', 'setXY': <function CC.setXY at 0x000000C3473DA048>, 'printXY': <function CC.printXY at 0x000000C3473C4F28>, '__dict__': <attribute '__dict__' of 'CC' objects>, '__weakref__': <attribute '__weakref__' of 'CC' objects>, '__doc__': None}

dd.setXY(4, 5)
print(dd.__dict__)
# {'x': 4, 'y': 5}

print(vars(CC))
# {'__module__': '__main__', 'setXY': <function CC.setXY at 0x000000632CA9B048>, 'printXY': <function CC.printXY at 0x000000632CA83048>, '__dict__': <attribute '__dict__' of 'CC' objects>, '__weakref__': <attribute '__weakref__' of 'CC' objects>, '__doc__': None}
9. 一些相关的内置函数(BIF)

issubclass(class, classinfo) 方法用于判断参数 class 是否是类型参数 classinfo 的子类。
一个类被认为是其自身的子类。
classinfo可以是类对象的元组,只要class是其中任何一个候选类的子类,则返回True

class A:
    pass


class B(A):
    pass


print(issubclass(B, A))  # True
print(issubclass(B, B))  # True
print(issubclass(A, B))  # False
print(issubclass(B, object))  # True
  • isinstance(object, classinfo) 方法用于判断一个对象是否是一个已知的类型,类似type()。
  • type()不会认为子类是一种父类类型,不考虑继承关系。
  • isinstance()会认为子类是一种父类类型,考虑继承关系。
  • 如果第一个参数不是对象,则永远返回False。
  • 如果第二个参数不是类或者由类对象组成的元组,会抛出一个TypeError异常。
a = 2
print(isinstance(a, int))  # True
print(isinstance(a, str))  # False
print(isinstance(a, (str, int, list)))  # True


class A:
    pass


class B(A):
    pass


print(isinstance(A(), A))  # True
print(type(A()) == A)  # True
print(isinstance(B(), A))  # True
print(type(B()) == A)  # False

hasattr(object, name)用于判断对象是否包含对应的属性。

class Coordinate:
    x = 10
    y = -5
    z = 0


point1 = Coordinate()
print(hasattr(point1, 'x'))  # True
print(hasattr(point1, 'y'))  # True
print(hasattr(point1, 'z'))  # True
print(hasattr(point1, 'no'))  # False

getattr(object, name[, default])用于返回一个对象属性值。

class A(object):
    def set(self, a, b):
        x = a
        a = b
        b = x
        print(a, b)


a = A()
c = getattr(a, 'set')
c(a='1', b='2')  # 2 1


setattr(object, name, value)对应函数 getattr(),用于设置属性值,该属性不一定是存在的。

class A(object):
    bar = 1


a = A()
print(getattr(a, 'bar'))  # 1
setattr(a, 'bar', 5)
print(a.bar)  # 5
setattr(a, "age", 28)
print(a.age)  # 28

delattr(object, name)用于删除属性

class Coordinate:
    x = 10
    y = -5
    z = 0


point1 = Coordinate()

print('x = ', point1.x)  # x =  10
print('y = ', point1.y)  # y =  -5
print('z = ', point1.z)  # z =  0

delattr(Coordinate, 'z')

print('--删除 z 属性后--')  # --删除 z 属性后--
print('x = ', point1.x)  # x =  10
print('y = ', point1.y)  # y =  -5

# 触发错误
print('z = ', point1.z)
# AttributeError: 'Coordinate' object has no attribute 'z'
  1. class property([fget[, fset[, fdel[, doc]]]])用于在新式类中返回属性值。
    • fget – 获取属性值的函数
    • fset – 设置属性值的函数
    • fdel – 删除属性值函数
    • doc – 属性描述信息
class C(object):
    def __init__(self):
        self.__x = None

    def getx(self):
        return self.__x

    def setx(self, value):
        self.__x = value

    def delx(self):
        del self.__x

    x = property(getx, setx, delx, "I'm the 'x' property.")


cc = C()
cc.x = 2
print(cc.x)  # 2

del cc.x
print(cc.x)
# AttributeError: 'C' object has no attribute '_C__x'

四、魔法方法

1. 基本的魔法方法(补)

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__
2. 算术运算符

类型工厂函数,指的是“不通过类而是通过函数来创建对象”

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(C))  # <class 'type'>
print(int('123'))  # 123

# 这个例子中list工厂函数把一个元祖对象加工成了一个列表对象。
print(list((1, 2, 3)))  # [1, 2, 3]

add(self, other)定义加法的行为:+
sub(self, other)定义减法的行为:-

注意: divmod(a, b)把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)。

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()
    ‘’‘
    __mul__(self, other)定义乘法的行为:*
	__truediv__(self, other)定义真除法的行为:/
	__floordiv__(self, other)定义整数除法的行为://
	__mod__(self, other) 定义取模算法的行为:%
	__divmod__(self, other)定义当被 divmod() 调用时的行为
	__pow__(self, other[, module])定义当被 power() 调用或 ** 运算时的行为
	__lshift__(self, other)定义按位左移位的行为:<<
	__rshift__(self, other)定义按位右移位的行为:>>
	__and__(self, other)定义按位与操作的行为:&
	__xor__(self, other)定义按位异或操作的行为:^
	__or__(self, other)定义按位或操作的行为:|
	’‘’
    print(divmod(7, 2))  # (3, 1)
	print(divmod(8, 2))  # (4, 0)
3. 反算术运算符

反运算魔方方法,与算术运算符保持一一对应,不同之处就是反运算的魔法方法多了一个“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
这里加数是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
4. 增量赋值运算
__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)定义赋值按位或操作的行为:|=
5. 一元运算符
__neg__(self)定义正号的行为:+x
__pos__(self)定义负号的行为:-x
__abs__(self)定义当被abs()调用时的行为
__invert__(self)定义按位求反的行为:~x
6. 属性访问
__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__
7. 描述符

描述符就是将某种特殊类型的类的实例指派给另一个类的属性。

  • 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>
8. 定制序列
  • 如果说你希望定制的容器是不可变的话,你只需要定义__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}

__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}
9. 迭代器

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

迭代器有两个基本的方法: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

在这里插入图片描述

把一个类作为一个迭代器使用需要在类中实现两个魔法方法 __iter__()__next__()1__iter__(self)定义当迭代容器中的元素的行为,返回一个特殊的迭代器对象, 这个迭代器对象实现了 __next__() 方法并通过 StopIteration 异常标识迭代的完成。
2__next__() 返回下一个迭代器对象。
3,StopIteration 异常用于标识迭代的完成,防止出现无限循环的情况,在 __next__() 方法中我们可以设置在完成指定循环次数后触发 StopIteration 异常来结束迭代。

class 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
10 生成器
  • 在 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

在这里插入图片描述

用生成器实现斐波那契数列。
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 21 34 55 89
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值