huangxz的笔记 https://bbs.21ic.com/?73035 [收藏] [复制] [RSS]

日志

python类new测试

已有 262 次阅读2021-11-27 21:28 |系统分类:兴趣爱好

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import ctypes
from ctypes import (
    Structure,
    WINFUNCTYPE,
    POINTER,
    cast,
    c_long,
    c_void_p,
    c_ulong,
    byref,
    pointer,
)

class BREAD:
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def create(self, a ,b):
        if a > b:
            return 0
        else:
            return 1
    def test(self):
        return 55

class FLAKE:
    def __init__(self, a, b):
        self.a = a
        self.b = b
        #if(a > b):
        #    self = None
    def __new__(cls,*args,**kwagrs):
        if args[0].create(3,5) > 0:
            return 42
        else:
            return super(FLAKE, cls).__new__(cls)
            
    def func1(self):
        return self.a.test() + self.b
        
        

class PASSTHRU_MSG(Structure):
    _fields_ = [
        ("ProtocolID", c_ulong),
        ("RxStatus", c_ulong),
        ("TxFlags", c_ulong),
        ("Timestamp", c_ulong),
        ("DataSize", c_ulong),
        ("ExtraDataindex", c_ulong),
        ("Data", ctypes.c_ubyte * 4128),
    ]
def test_msg():
    a=PASSTHRU_MSG(ProtocolID=3,RxStatus=5)
    x = (PASSTHRU_MSG*2)()
    x[0]=a
    print(x)
    
if __name__ == "__main__":
    test_msg()
    x = BREAD(3,5)
    flake = FLAKE(x, 6)
    if type(flake) == FLAKE:
        print(flake.func1())
    else:
        print("can't create object")
       

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)