阿里云天池Python训练营-打卡3


一、函数参数

  • 可变参数 *args 自动组装成元组
  • 关键字参数 **kwargs 自动组装成字典

二、匿名函数应用

odd = lambda x : x%2 == 1
oddlist = filter(odd, [1,2,3,4,5,6,7])
print(list(oddlist))

m2 = map(lambda x, y, z: x + y-z, [1, 3, 5, 7, 9, 10], [2, 4, 6, 8, 10, 11], [1,2,3,4])
print(list(m2))
输出:[1, 3, 5, 7]
     [2, 5, 8, 11]

三、类的继承

class MyList(list):
    pass

四、property

class Screen(object):
    @property
    def width(self):
        return self._width

    @property  # 这个修饰器可以把getter方法变成属性
    def height(self):
        return self._height

    @width.setter  # 定义width的setter方法
    def width(self, value):
        self._width = value

    @height.setter
    def height(self, value):
        self._height = value

    @property
    def resolution(self):
        return self._height * self._width
s = Screen()
s.width=1024 # 调用了width的setter方法
s.height=768
print(s.resolution) # 调用了resolution的getter方法
输出:786432

五、魔法方法

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

# __str__(self)方法,当你打印一个对象的时候,触发__str__;当你使用%s格式化的时候,触发__str__;str强转数据类型的时候,触发__str__
# __repr__(self)方法,没有实现__str__的时候,执行__repr__;repr(obj)内置函数对应的结果是__repr__的返回值;当你使用%r格式化的时候 触发__repr__

# __add__(self, other)定义加法的行为:+
# __sub__(self, other)定义减法的行为:-
# __mul__(self, other)定义乘法的行为:*
# __truediv__(self, other)定义真除法的行为:/
# __floordiv__(self, other)定义整数除法的行为://
# __mod__(self, other) 定义取模算法的行为:%
# __divmod__(self, other)定义当被 divmod(){返回一个包含商和余数的元组(a // b, a % b)}调用时的行为

# __pow__(self, other[, module])定义当被 power() 调用或 ** 运算时的行为
# __lshift__(self, other)定义按位左移位的行为:<<
# __rshift__(self, other)定义按位右移位的行为:>>
# __and__(self, other)定义按位与操作的行为:&
# __xor__(self, other)定义按位异或操作的行为:^
# __or__(self, other)定义按位或操作的行为:|

# 反运算魔方方法,与算术运算符保持一一对应,不同之处就是反运算的魔法方法多了一个“r”

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

# __get__(self, instance, owner)用于访问属性,它返回属性的值。
# __set__(self, instance, value)将在属性分配操作中调用,不返回任何内容。
# __del__(self, instance)控制删除操作,不返回任何内容。

# __len__(self)定义当被len()调用时的行为(返回容器中元素的个数)
# __getitem__(self, key)定义获取容器中元素的行为,相当于self[key]
# __setitem__(self, key, value)定义设置容器中指定元素的行为,相当于self[key] = value
# __delitem__(self, key)定义删除容器中指定元素的行为,相当于del self[key]
输出:I LOVE LSGOGROUP
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值