python44 45 简单定制 属性访问

# 45 属性访问
class Rectangle:
    def __init__(self,width=0,height=0):
        self.width = width
        self.height = height
    def __setattr__(self,name,value):
        if name == 'square':
            self.width = value
            self.height= value
        else:
            #self.__dict__[name] = value
            #self.name = value                 #会陷入死循环 该句还会去调用 setattr --》 再 调 该句 --
            #super().__setattr__(name,value)   # 调用基类的方法 可行 但why 没编译过 ?
    def getArea(self):
        return self.width * self.height

# 44 简单定制  类的定制  :计时器
func:
    start
    stop
    start+ stop:
'''
class B():
    def __str__(self):    # b=B() print(b)
    #def __repr__(self):  # b=B() b
        return "大大"
'''
'''
import time as t
class MyTime():
    #开始计时
    def start(self):
        self.start = t.localtime()
        print("计时开始。。。")
    # 停止挤时
    def stop(self):
        self.stop = t.localtime()
        self._calc()
        print("计时停止")
    # 内部方法
    def _calc(self):
        self.lasted=[]
        self.prompt = "总共运行了"
        for index in range(6):
            self.lasted.append(self.stop[index] - self.start[index])
            self.prompt+=str(self.lasted[index])
        print(self.prompt)
'''
'''
import time as t
class MyTime():
    
    def __str__(self):
        return self.prompt
    __repr__ = __str__
    
    #开始计时
    def start(self):
        self.start = t.localtime()
        print("计时开始。。。")
        
    # 停止挤时
    def stop(self):
        self.stop = t.localtime()
        self._calc()
        print("计时停止")
        
    # 内部方法
    def _calc(self):
        self.lasted=[]
        self.prompt = "总共运行了"
        for index in range(6):
            self.lasted.append(self.stop[index] - self.start[index])
            self.prompt+=str(self.lasted[index])
        #print(self.prompt)
#-----------------------------
>>> t1 = MyTime()
>>> t1.start()
计时开始。。。
>>> t1.stop()
计时停止
>>> t1
总共运行了000003
>>> t1 = MyTime()
>>> t1

Traceback (most recent call last):
  File "<pyshell#70>", line 1, in <module>
    t1
  File "C:/Users/A23827359/Desktop/03201c", line 33, in __str__
    return self.prompt
AttributeError: MyTime instance has no attribute 'prompt'
solution: 用init 定义之
'''

'''
import time as t
class MyTime():
    def __init__(self):
        self.prompt="未开始计算时间"
        self.lasted=[]
        self.start=0
        self.stop=0
        
    def __str__(self):
        return self.prompt
    __repr__ = __str__
    
    #开始计时
    def start(self):
        self.start = t.localtime()
        print("计时开始。。。")
        
    # 停止挤时
    def stop(self):
        self.stop = t.localtime()
        self._calc()
        print("计时停止")
        
    # 内部方法
    def _calc(self):
        self.lasted=[]
        self.prompt = "总共运行了"
        for index in range(6):
            self.lasted.append(self.stop[index] - self.start[index])
            self.prompt+=str(self.lasted[index])
        #print(self.prompt)
#---------
>>> t1 = MyTime()
>>> t1.start()

Traceback (most recent call last):
  File "<pyshell#76>", line 1, in <module>
    t1.start()
TypeError: 'int' object is not callable  #整形不可以被调用 可我调用的是方法啊  因为你在init 里定义了他们 start是属性了 覆盖了start方法
'''
'''
# 更正:
import time as t
class MyTime():
    def __init__(self):
        self.prompt="未开始计算时间"
        self.lasted=[]
        self.begin=0
        self.end=0
        
    def __str__(self):
        return self.prompt
    __repr__ = __str__
    
    #开始计时
    def start(self):
        self.begin = t.localtime()
        print("计时开始。。。")
        
    # 停止挤时
    def stop(self):
        self.end = t.localtime()
        self._calc()
        print("计时停止")
        
    # 内部方法
    def _calc(self):
        self.lasted=[]
        self.prompt = "总共运行了"
        for index in range(6):
            self.lasted.append(self.end[index] - self.begin[index])
            self.prompt+=str(self.lasted[index])
        #print(self.prompt)
>>> t1 = MyTime()
>>> t1.start()
计时开始。。。
>>> t1.stop()
计时停止
>>> t1
总共运行了000008
'''
'''
# 改进000008 为 8s
import time as t
class MyTime():
    def __init__(self):
        self.unit = ['年','月','日','时','分','秒']   #------ +
        self.prompt="未开始计算时间"
        self.lasted=[]
        self.begin=0
        self.end=0
        
    def __str__(self):
        return self.prompt
    __repr__ = __str__
    
    #开始计时
    def start(self):
        self.begin = t.localtime()
        print("计时开始。。。")
        
    # 停止挤时
    def stop(self):
        self.end = t.localtime()
        self._calc()
        print("计时停止")
        
    # 内部方法
    def _calc(self):
        self.lasted=[]
        self.prompt = "总共运行了"
        for index in range(6):
            self.lasted.append(self.end[index] - self.begin[index])
            if self.lasted[index]:                                         #若时间为0 则不安此格式显示
                self.prompt+=(str(self.lasted[index])+self.unit[index])     # add
        #print(self.prompt)
'''
# 改进加判断
import time as t
class MyTime():
    def __init__(self):
        self.unit = ['年','月','日','时','分','秒']   #------ +
        self.prompt="未开始计算时间"
        self.lasted=[]
        self.begin=0
        self.end=0
        
    def __str__(self):
        return self.prompt
    __repr__ = __str__
    
    def __add__(self,other):
        prompt ="总共运行了"
        result = []
        for index in range(6):
            result.append(self.lasted[index] + self.lasted[index])
            if result[index]:
                prompt+=(str(result[index])+self.unit[index])
            return prompt
    
    #开始计时
    def start(self):
        self.prompt = "提示: 先运行stop()停止计时"
        self.begin = t.localtime()
        print("计时开始。。。")
        
    # 停止挤时
    def stop(self):
        if not self.begin:
            print("提示: 请先用start()开始计时")
        else:
            self.end = t.localtime()
            self._calc()
            print("计时停止")
        
    # 内部方法
    def _calc(self):
        self.lasted=[]
        self.prompt = "总共运行了"
        for index in range(6):
            self.lasted.append(self.end[index] - self.begin[index])
            if self.lasted[index]:                                    #若时间为0 则不安此格式显示
                self.prompt+=(str(self.lasted[index])+self.unit[index])     # add
        #为下一轮计算初始化
        self.begin = 0
        self.end =0
         
————————————————
版权声明:本文为CSDN博主「qq_23183809」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_23183809/article/details/105206191

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值