42讲::
0:就是类
1:__sub__
2:名字重复不规范把
3:
4:很随意的风格 这叫做鸭子风格。
5:
class Nstr(str): def __add__(self,other): return str.__add__(self,other) def __sub__(self,str1): return self.replace(str1,‘‘)
6:
class Nstr(str): def __lshift__(self,other): if other>len(self): other = other%len(self) str1 = self[0:other] str2 = self[other:len(self)] return (str2 +str1) a = Nstr(‘1234567a‘) print(a<<3)
7:
class Nstr(int): def __new__(cls, arg): if isinstance(arg,str): total = 0 for each in arg: total += (each) else: print(‘参数错误‘) return int.__new__(cls,total)
43:;
0:不会
1:当前面的没有对应方法时
2:直接调用xxxxxxxxxx 用super来调用
3:用函数的思想 为类取一个名字 当需要修改时 修改名字所对应的类即可
4:
class C: count = 0 # 静态属性 def __init__(self): C.count = C.count + 1 # 类名.属性名的形式引用 def getCount(self): return C.count
静态属性就是 不在方法里定义的 直接用类+.就可以访问
5:静态方法就是把一个方法变为静态。这样整个程序里只有一个,与具体的对象无关了。(实例化的对象的这个方法都会指向同一个地址的方法。)
6:
class C: def __init__(self,*para): if not para: print(‘并没有传入参数‘) else: count = 0 for each in para: count += 1 print(‘传入了%d个参数‘%count) c = C(1,2,3)
原来这里需要用 not 来判断 我等了半天的NOne
7:
class word(str):
def __init__(self,str1):
for i,each in enumerate(str1) :
if each == ‘ ‘:
self.str_ = str1[0:i]
break
def __lt__(self, other):
print(len(self.str_))
print(len(other.str_))
if len(self.str_)<len(other.str_):
return 1
else:
return 0
a = word(‘dsa dasd‘)
b = word(‘ddasdas dasd‘)
print(a<b)
很随意的写了一下 不要在意漏洞
44讲!!!!!!!!:
0: 我只能想到笨方法。。。
import time class mytimer(): def __init__(self): self.start = 0 self. end = 0 self.prompt = ‘未开始计时‘ def __str__(self): return self.prompt __repr__ = __str__ def begin(self): self.start = time.localtime() print(‘开始计时‘) def stop(self): self.end = time.localtime() print(‘结束计时‘) self.cal() def cal(self): lasted = [] unit = [‘年‘,‘月‘,‘日‘,‘时‘,‘分‘,‘秒‘] self.prompt = ‘总共运行了‘ for i in range(6): lasted.append(self.end[i]-self.start[i]) if lasted[5]<0: #这里我想着因为借位不一样,所以循环不好写,但是我忘了可以把借位放在列表里啊 意识还是不到位。 lasted[4] -= 1 lasted[5] += 60 if lasted[4]<0: lasted[3] -= 1 lasted[4] += 60 if lasted[3]<0: lasted[2] -= 1 lasted[3] += 24 if lasted[2]<0: lasted[1] -= 1 lasted[2] += 30 if lasted[1]<0: lasted[0] -= 1 lasted[6] += 12 for i in range(6): self.prompt += (str(lasted[i])+unit[i]) print(self.prompt) time1 = mytimer() time1.begin() k = 0 for i in range(1000000000): k = i time1.stop() print(time1)
1 : 对不起 我没看懂题目 但我看懂答案了
import time as t class MyTimer: def __init__(self): self.prompt = "未开始计时!" self.lasted = 0.0 self.begin = 0 self.end = 0 self.default_timer = t.perf_counter def __str__(self): return self.prompt __repr__ = __str__ def __add__(self, other): result = self.lasted + other.lasted prompt = "总共运行了 %0.2f 秒" % result return prompt # 开始计时 def start(self): self.begin = self.default_timer() self.prompt = "提示:请先调用 stop() 停止计时!" print("计时开始...") # 停止计时 def stop(self): if not self.begin: print("提示:请先调用 start() 进行计时!") else: self.end = self.default_timer() self._calc() print("计时结束!") # 内部方法,计算运行时间 def _calc(self): self.lasted = self.end - self.begin self.prompt = "总共运行了 %0.2f 秒" % self.lasted # 为下一轮计时初始化变量 self.begin = 0 self.end = 0 # 设置计时器(time.perf_counter() 或 time.process_time()) def set_timer(self, timer): if timer == ‘process_time‘: self.default_timer = t.process_time elif timer == ‘perf_counter‘: self.default_timer = t.perf_counter else: print("输入无效,请输入 perf_counter 或 process_time")
2:
import time as t class MyTimer: def __init__(self,fun,count=1000000): self.prompt = "未开始计时!" self.lasted = 0.0 self.begin = 0 self.end = 0 self.default_timer = t.perf_counter self.start() for i in range(count): fun() self.stop() def __str__(self): return self.prompt __repr__ = __str__ def __add__(self, other): result = self.lasted + other.lasted prompt = "总共运行了 %0.2f 秒" % result return prompt # 开始计时 def start(self): self.begin = self.default_timer() self.prompt = "提示:请先调用 stop() 停止计时!" print("计时开始...") # 停止计时 def stop(self): if not self.begin: print("提示:请先调用 start() 进行计时!") else: self.end = self.default_timer() self._calc() print("计时结束!") # 内部方法,计算运行时间 def _calc(self): self.lasted = self.end - self.begin self.prompt = "总共运行了 %0.2f 秒" % self.lasted # 为下一轮计时初始化变量 self.begin = 0 self.end = 0 # 设置计时器(time.perf_counter() 或 time.process_time()) def set_timer(self, timer): if timer == ‘process_time‘: self.default_timer = t.process_time elif timer == ‘perf_counter‘: self.default_timer = t.perf_counter else: print("输入无效,请输入 perf_counter 或 process_time") def fun1(): i=0 k=i time1 = MyTimer(fun1,10000000) print(time1)
45讲啦啊啊啊啊啊啊 :
1:作用是定义赋值时的动作吧。
会出现无限循环把。
原来如此啊 ,原来下面setattr 中的key 指的就是等号前面那个 那个value 就是等号后面那个 哎呀 终于知道了
class test: def __init__(self,key1): self.key1 = 2 def __setattr__(self, key, value): self.__dict__[key] = value+1 test1 = test(2) print(test1.key1)
2:__getattr__ 对不起 我错了 是 ____getattribute__(self, name)R
3: 3
1,1 :2,None
3:离谱:
>>> c = C() >>> c.x 2 1 Traceback (most recent call last): File "<pyshell#31>", line 1, in <module> c.x File "<pyshell#29>", line 4, in __getattr__ return super().__getattr__(name) AttributeError: ‘super‘ object has no attribute ‘__getattr__‘
4:还没有counter
5:哈哈哈哈哈哈哈哈哈哈哈哈哈哈, 这个笑点实在是太有了!!!
try: class C(): def __init__(self): self.key = 1 c = C print(C.aaa) except AttributeError: print(‘该属性不存在‘)
class C: def __init__(self): self.key = 1 def __getattr__(self, item): return ‘该对象不存在‘ c = C() print(c.aaa)
6:
class DEMO: def __getattr__(self, item): self.item = ‘FishC‘ return self.item demo = DEMO() print(demo.x) demo.x = "X-man" print(demo.x)
你这跟要求不一样啊 少单引号
7:
class Counter: def __init__(self): self.__dict__[‘counter‘] = 0 def __setattr__(self, name, value): self.__dict__[‘counter‘] += 1 super().__setattr__(name, value) def __delattr__(self, name): self.counter -= 1 super().__delattr__(name) c = Counter() c.x = 1 print(c.counter) c.y = 1 c.z = 1 print(c.counter) del c.x print(c.counter)