你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

python3 语法再学习

2021/12/17 21:06:53
import os
import struct

class debug:
    __file = ""
    def __init__(self,filename) -> None:
        self.file = filename
        print("debug father call "+filename+" \n")
    def smeth():
        print("Hello static method\n")
    staticmethod(smeth)

#使用父类的构造函数
class test(debug):
    def __init__(self,name) -> None:
        super().__init__(name)
        print("hello I am child for debug "+ self.file)


# python 的构造函数和非构造函数  使用方法是一致的
# supper 函数的使用会更加直观

def print_test(func):
    def nidaye():
        print("start ... ...\n")
        func()
        print("end ... ...\n")
    return nidaye

@print_test
def woshinidaye():
    print("good\n")


#需要调用方法才可以使用,property 的这个属性

class func_t:
    w=0
    h=0
    @classmethod
    def nihao(self):
        print("ni hao\n")
    def get_attr(self):
        return self.w,self.h
    def set_attr(self,all_attr):
        self.w,self.h = all_attr

    all_attr=property(get_attr,set_attr) #获取相关的函数参数种类,将size 关联到这个特性进行使用,先Set 然后在 Get 进行不同的返回,使用和设置不同的方法,返回部分的函数属性

    # 这可以触发一个迭代器
    # 构建和使用一个迭代器

    def __iter__(self):
        return self

    def __next__(self):             #查看一个迭代器的设置
        print("这是一个迭代器")

def create_iter(num):
    for s in num:
        for p in s:
            yield p #冻结跳出函数,直到下一个继续执行


if __name__ == '__main__':
    test_class = test("hello_world")
    woshinidaye()
    func_t.nihao()
    good = func_t()
    print(good.all_attr)
    it = good.__iter__()
    it.__next__()
    array = [1,2,3,4]
    it = array.__iter__()
    
    # while True:
    #     f = next(it,None) #使用next 迭代访问 数组的关系,常用的迭代器的访问方法
    #     if f == None:
    #         break
    #     print(f)
    # 返回的是int clas 的对象

    test_v1 = [[1],[2,3,4],[5,6]]
    for tmp in create_iter(test_v1):
        print(type(tmp))
    
    # 有之前的一个变成一群的生成器,这个就很好用
    test_v2 = create_iter(test_v1)

    #可以使用迭代器访问生成器
    cc = next(create_iter(test_v1))
    print(cc)