Python笔记,面向对象(10)内置属性

常用内置属性

类属性

_ _dict _ _:类的属性

_ _ bases _ _:类的所有父类构成元组

_ _ doc _ _:类的文档字符串

_ _ module _ _:类定义所在的模块

实例属性

_ _ dict _ _:实例的属性

_ _ class _ _:实例对应的类

内置特殊方法

信息格式化

__str__方法

可以使用一个字符串,来描述由这个类产生的实例,str方法为面向用户,触发的方法有两种,一种是直接通过print函数,另外一种是通过str函数进行转化

class Person:
    def __init__(self, n, a):
        self.name = n
        self.age = a

    def __str__(self):
        return "这个人的姓名是%s, 这个人的年龄是%s"%(self.name, self.age)

p1 = Person("sz", 18)
# print(p1.name)
# print(p1.age)
print(p1)

p2 = Person("wm", 77)
# print(p2.name)
# print(p2.age)
print(p2)

s = str(p1)
print(s, type(s))



#这个人的姓名是sz, 这个人的年龄是18
#这个人的姓名是wm, 这个人的年龄是77
#这个人的姓名是sz, 这个人的年龄是18 <class 'str'>

_ _repr _ _方法

面向开发人员,触发的方法有两种,一种是直接通过repr函数,另外一种是通过交互模式里面直接把对象对应的变量名称直接敲回车,会找方法的返回值,打印出来。

class Person:
    def __str__(self):
        return
    def __repr__(self):
        return
    p = Person()
    print(p)
str
    p
repr    
    

调用操作

__ call __

​ 作用:使得"对象"具备当做函数,来调用的能力

​ 使用:通过一个类实例化一个对象,可以直接拿这个对象去调用,会自动找一个叫做call的实例方法。

class Person:
    def __call__(self, *args, **kwargs):
        print("xxx", args, kwargs)
    pass

p = Person()
p(123, 456, name = "sz")


#xxx (123, 456) {'name' : 'sz'}

应用场景

#创建很多歌画笔,画笔的类型(钢笔,铅笔),画笔的颜色(红,黄,青,绿)

class PenFactory:
    def __init__(self, p_type):
        self.p_type =p_type

    def __call__(self, p_color):
        print("创建了一个%s这个类型的画笔,它是%s颜色" % (self.p_type, p_color)

gangbiF = PenFactory("钢笔")
gangbiF("红色")
gangbiF("黄色")

qianbiF = PenFactory("铅笔")
qianbiF("绿色")
qianbiF("蓝色")

索引操作

作用:可以对一个实例对象进行索引操作

步骤:

​ 1.实现三个内置方法

​ 2.可以以索引的形式操作对象
在这里插入图片描述

class Person:
    def __init__(self):
        self.cache = {}

    def __setitem__(self, key, value):
        # print("setitem", key, value)
        self.cache[key] = value

    def __getitem__(self, item):
        # print("getitem", item)
        return self.cache[item]

    def __delitem__(self, key):
        #print("delitem", key)
        del self.cache[key]

p = Person()
p["name"] = "sz"

print(p["name"])

del p["name"]

print(p.cache)
# 

切片操作

  • 作用:可以对一个实例对象进行切片操作

  • 步骤:

    • Python2.x:

      ​ 1.实现三个内置方法

      ​ _ _ setspice _ _:设置某个元素切片时调用

      ​ _ _ getspice _ _:获取某个元素切片时调用

      ​ _ _ delspice _ _:删除某个元素时调用

      ​ 2.可以直接按照切片的方式操作对象:p[1,6,2]

      ​ 3.注意:这三种方法已过期

    • Python3.x:同一由"索引操作"进行管理
      ​ def _ _ setitem_ _ _(self, key, value):
      def _ _ getitem _ _(self, item):
      def _ _ delitem _ _(self, key):

class Person:                                 
    def __init__(self):                       
        self.items = [1,2,3,4,5,6,7,8]        
                                              
    def __setitem__(self, key, value):        
        # print(key, value)                   
        # print(key.start)                    
        # print(key.stop)                     
        # print(key.step)                     
        # print(value)                        
        self.items[key] = value               
                                              
    def __getitem__(self, item):              
        print("getitem", item)                
                                              
    def __delitem__(self, key):               
        print("delitem", key)                 
                                              
p = Person()                                  
p[0: 4: 2] = ["a", "b"]                       
print(p.items)                                
                                              
del p[0: 5: 2]                                
                                              

比较操作

作用:可以定义对象"比较大小,相等以及真假"规则

  • 步骤:实现六个方法

    ​ 相等:_ _eq _ _

    ​ 不等于:_ _ ne _ _

    ​ 小于:_ _ lt _ _

    ​ 小于或等于:_ _ le _ _

    ​ 大于:_ _ ge _ _

    ​ 大于或等于: _ _ ge _ _

class Person:
    def __init__(self, age, height):
        self.age = age
        self.height = height

    def __eq__(self, other):   #等于
        print(other)
        return self.age  == other.age

    def __ne__(self, other):   #不等于
        print(other)
        return self.age != other.age

    def __gt__(self, other):   #大于
        print(other)
        return self.age > other.age

    def __ge__(self, other):   #大于等于
        pass

    def __lt__(self, other):   #小于
        pass

    def __le__(self, other):   #小于等于
        pass

p1 = Person(14, 458)
p2 = Person(14, 458)

print(p1 == p2)

注意

​ 如果对于反向操作的比较符,只定义了其中一个方法,但使用的是另一种比较运算,那么解释器会采用调换参数的方式进行调用该方法

​ 但是,不支持叠加操作

class Person:
    def __init__(self, age, h):
        self.age = age
        self.h = h

    def __lt__(self, other):
        # print("lt")
        print(self.age)
        print(other.age)
        return self.age < other.age

p1 = Person(14, 78)
p2 = Person(41, 75)
# print(p1 > p2)  如果采用反向操作符,此时self和other会互换
print(p1 < p2)

补充

import functools

@functools.total_ordering    #该装饰器会自动帮我们补全其他方法
class Person:
    def __lt__(self, other):
        print("lt")
        pass
        return true

    def __eq__(self, other):
        print("eq")
        pass

p1 = Person()
p2 = Person()

print(p1 <= p2)

print(Person.__dict__)


#其中 <=为或操作,当第一步的lt已经实现的时候,下一步的eq就不用去执行了。
lt
True

上下文布尔值

class Person:
    def __init__(self):
        self.age = 17

    def __bool__(self):
        return self.age >= 18
    pass

p = Person()

if p:
    print("已满十八岁")
else:
    print("未满十八岁")

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值