PYTHON _笔记_040-47节

#类只要实例化后就跟原来的类没什么关系了,创建出来的实例对象只有文件关闭才会消失
issubclass(B, A) #A是B的子类吗? 自身是自身的子类,所有类的基类是object
issubclass(B, object) #返回True
class A:
    pass
class B(A):
    pass
class c:
    pass
b1 = B()
isinstance(b1, A) #返回True,因为B继承了A,所以b1也是A的实例化对象
isinstance(b1, C) #返回False
isinstance(b1, (A, B, C)) #可以接受一个元组,元组里的类有就可以
hasattr() #has-attribute 检测对象是否含有某个属性
class C:
    def __init__(self, x=0):
        self.x = x
c1 = C()
hasattr(c1, 'x') #要变字符串,否则以为是变量
getattr(c1, 'y', "您所访问的属性不存在...") #得到对象某个属性的值
setattr(c1, 'y', 'FishC') #在对象里设定一个属性并赋值
getattr(c1, 'y', "您所访问的属性不存在...")
delattr(c1, 'y') #删除指定的属性
class C:
    def __init__(self, size = 10):
        self.size = size
    def getSize(self):
        return self.size
    def setSiZe(self, size):
        self.size = size
    def delSiZe(self, size):
        del self.size
    X = property(getSize, setSiZe, delSiZe)
c1 = C()
print(c1.X)
print(c1.size)
del c1.X
c1.X
#__init__方法里面不要写返回
class Capstr(str):
    def __new__(cls, string) #cls这里指向str方法
        string = string.upper()
        return str.__new__(cls, string)

a = Capstr("i love fishc.com")
print(a)
class C:
    def __init__(self):
        print("我是init方法,我被调用了...")
    def __del__(self):
        print("我是del方法,我被调用了...")
c1 = C()
c2 = c1
c3 = c2
#这是变量的赋值,有三个实例对象都指向C,只有当同类对象全部消亡时,才会调用__del__方法
del c1
del c2
del c3
type(len)/type(dir)/#都是内置函数 一种方法
type(int)/type(list) #都是类
a = int('123') #int是类对象,123是参数,返回实例对象int的值赋给a,而不是把字符转成数字
a + b #两个实例对象相加
__add__/__sub__ #相减
class New_int:
    def __add__(self, other):
        return int.__sub__(self, other)
    def __sub__(self, other):
        return int.__add__(self, other)
a = New_int(3)
b = New_int(5)
print(a+b) #输出为-2

class Try_int(int):
    def __add__(self, other):
        return self + other
    def __sub__(self, other):
        return self - other
#发生递归,return的self已经是一个方法了,自己不断调用自己
#__mul__ 乘法;__truediv__ 真除法,得到一个小数;__floordiv__ 地板除法,小数去掉
#__mod__ 取余 __divmod__ 取余之后得到一个元组
#__lshift__ 按位左移 __lright__ 按位右移 __and__按位与 __xor__按位异或 __or__按位或 
class Nint(int):
    def __radd__(self, other):
        return int.__sub__(self, other)
#__neg__(self) 正号;__pos__(self) 负号;__abs__(self) 绝对值;__invert__(self)按位求反
#python 没有声明,只有定义
import time as t
class MyTimer:
    def __init__(self):
        self.unit = ["年","月","日","时","分","秒"]
        self.prompt = "还没有计时!!"
        self.listed = []
        self.start = 0
        self.stop = 0
    def __str__(self):
        return str.prompt
    __repr__ = __str__
    def begin(self):
        self.start = t.localtime()
        self.prompt = "请先调用end()结束计时..."
        print("计时开始!")
    def end(self):
        if not self.start:
            print("请先调用start()进行计时!")
        self.stop = t.localtime()
        self._calc()
        print("计时结束...")
    def _clac(self):
        self.listed = []
        self.prompt = "总共计时..."
    for index in range(6):
        self.listd.append(self.stop[index] - self.start[index])
        self.prompt += (str(self.listed[index]) + self.unit[index])
    #为下一次计时做准备
    self.start = 0
    self.stop = 0
    def __add__(self, other):
        prompt = "两段代码总共运行了"
        rusult = []
        for index in range(6):
            result.append(self.listed[index] + other.listed[index])
            if(result[index]):
                prompt += (str(result[index]) + self.unit(index))
        return prompt
        
class C:
    def __getattribute__(self, name):
        print("getattribute")
        return super().__getattribute__(name)
    def __getattr__(self, name)
        print("getattr")
        super().__getattr__(name)
    def __setattr__(self, name, value)
        print("setattr")
        super().__setattr__(name, value)
    def __delattr__(self, name)
        print("delattr")
        super().__delattr__(name)
c = C()
c.x #先访问getattribute,当用户试图获取一个不存的属性时,访问getattr
c.x = 1
delc.x

    
class Rectangle:
    def __init__(self, width=0, height=0):
        self.width = width
        self.height = height
    def __setattr__(self, name, value):
        if name == "square":
            self.height = value
            self.width = value
        else:
            super().__setattr__(name, value)
    def getArea(self):
        return self.width * self.height
 class MyDecripter:
    def __get__(self, instance, owner):
        print("getting", self, instance, owner)
    def __set__(self, instance, value):
        print("setting", self, instance, value)
    def __delete__(self, instance):
        print("delling", self, instance) 
class Test:
    x = MyDecriptor() 
===========================
class Celsius:
    def __init__(self, value = 26.0)
        self.value = float(value)
    def __get__(self, instance, owner):
        return self.value
    def __set__(self, instance, value):
        self.value = float(value)
    def __delete__(self, instance):
        print("delling", self, instance) 
class Fahrenheit:
    def __get__(self, instance, owner):
        return instance.cel * 1.8 +32
    def __set__(self, instance, value):
        instance.cel = (float(value) - 32) / 1.8
   
class Temprature:
    cel = Celsius()
    fah = Fahrenheit()
===============================
class Countlists:
    def __init__(self, *args):#不确定变量用#号
        self.values = [x for x in args]
#列表推导式,相当于for x in args[self.values.append(x)]
        self.count = {}.fromkeys(range(len(self.values), 0))
#创建一个新字典,键为range(len(self.values),0,1,2,3,4,5
    def __len__(self, values):
        return len(self.values) #返回值的长度
    def __getitem__(self, keys):
        self.count[keys] += 1
        return self.values[key]




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王泽邦_bill

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值