Python:获取对象信息的函数、运算符、输出列表中元素访问次数、迭代器

%r

%r是一个万能的格式符,它会将后面给的参数原样打印出来,带有类型信息

获取对象信息的函数type()、isinstance()、dir()

1.使用type()函数可以判断对象的类型,如果一个变量指向了函数或类,也可以用type判断;
2.对于有继承关系的类,我们要判断该类的类型,可以使用isinstance()函数,isinstance()判断的是一个对象是否是该类型本身,或者位于该类型的父继承链上;
3.能用type()判断的基本类型也可以用isinstance()判断,并且还可以判断一个变量是否是某些类型中的一种;
4.如果要获得一个对象的所有属性和方法,可以使用dir()函数,它返回一个包含字符串的list;

下面的代码是等价的

print(len('abc'))
print('abc'.__len__())

算术运算符

__add__(self, other) # 定义加法的行为:+
__sub__(self, other) # 定义减法的行为:a-b,b为被减数,a为减数
__mul__(self, other) # 定义乘法的行为:*
__truediv__(self, other) # 定义真除法的行为:/
__floordiv__(self, other) # 定义整数除法的行为://
__mod__(self, other)  # 定义取模算法的行为:%
__divmod__(self, other) # 定义当被 divmod() 调用时的行为
divmod(a, b) # 把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)
__pow__(self, other[, module]) # 定义当被 power() 调用或 ** 运算时的行为
__lshift__(self, other) # 定义按位左移位的行为:<<
__rshift__(self, other) # 定义按位右移位的行为:>>
__and__(self, other) # 定义按位与操作的行为:&
__xor__(self, other) # 定义按位异或操作的行为:^
__or__(self, other) # 定义按位或操作的行为:|

反算术运算符

__radd__(self, other) # 定义加法的行为:+
__rsub__(self, other) # 定义减法的行为:a-b,a为被减数,b为减数
__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) # 定义按位或操作的行为:|

增量赋值运算符

__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)v定义赋值按位右移位的行为:>>=
__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 CountList:
    def __init__(self, *args):
        self.values = [x for x in args]
        self.count = {
    
    }.fromkeys(range(len(self.values)), 0)  #设定键的初始值为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]    # 若没有for循环,则结果为{0: 0, 1: 2, 2: 0, 3: 0}
print(c1.count)
# {0: 0, 1: 0, 2: 0, 3: 0}

迭代器

iter()next() #迭代器两个基本的方法
iter(object)  # 函数用来生成迭代器
next(iterator[, default])  # 返回迭代器的下一个项目
iterator  #  可迭代对象
default  #  可选,用于设置在没有下一个元素时返回该默认值,如果不设置,又没有下一个元素则会触发 StopIteration 异常

猜你喜欢

转载自blog.csdn.net/xiaokeaiuiya/article/details/108583219
今日推荐