• python进阶笔记


    1. 基础知识 

      1.深浅拷贝

    # 2 深浅拷贝测试
    import copy
    
    a = [1, 2, 3, 4]
    # b = a # 将b发生改变时,a也会发生改变 
    b =  copy.deepcopy(a) # 表示进行浅拷贝
    b[2] = 5
    print(a)
    print(b)

      2. vars 将类里面的属性做成字典的形式 

    class A():
        def __init__(self, age, name):
            self.age = age
            self.name = name
    
    B = A(12, 'alex')
    for k, v in sorted(vars(B).items()):
        print(k, v)

    2.@修饰函数类别 

      1.@property 将修饰的类函数可以直接使用s.num 直接调用num函数

    # @property 可以把一种属性变成一种方法进行使用
    class Test(object):
    
        def __init__(self):
            self.__num = 100
        @property
        def num(self):
            return self.__num
    
        @num.setter #相当于将num获得的数据再传入到num里面进行后续的操作
        def num(self, num):
            self.__num = num
            pass
    
    t = Test()
    t.num = 50
    print(t.num)

      2.@staticmethod  类不实例化,内部的函数也可以被调用

    class A():
        def __init__(self):
            pass
        @staticmethod
        def print_A():
            print('A')
    
    A.print_A()

      3.@abstractmethod修饰的函数,不能通过实例化类来进行调用

    from abc import ABC, abstractmethod
    
    class Foo(ABC):
        @abstractmethod
        def func(self):
            print('fun in Foo')
    class SubFoo(Foo):
    
        def func(self):
            print("fun in SubFoo")
    
    f = Foo()
    f.func() # error

    3. __?__ 私有函数类别 

      1.__iter__  对实例化的类进行循环时,将进入到这个函数里面,返回类需要的函数值

    class Animal(object):
    
        def __init__(self, animal):
            self.animal = animal
    
        def __iter__(self):
            for data in (self.animal):
                yield data
    
        def __len__(self):
            return len(self.animal)
    
    animals = Animal(['dog', 'cat', 'fish'])
    
    for index, animal in enumerate(animals):
        print(animal)

      2. __getitem__ 当对类进行循环时,将会进入到__getitem__函数里面,这个函数的好处,可以传入index,返回下标指定的数据

    class Animal(object):
    
        def __init__(self, animal):
            self.animal = animal
    
        def __getitem__(self, item):
                return self.animal[item]
    
        def __len__(self):
            return len(self.animal)
    
    animals = Animal(['dog', 'cat', 'fish'])
    
    for index, animal in enumerate(animals):
        print(animal)  

      3.__len__ 当对类使用len时,返回的是这个函数的结果

    class Animal(object):
    
        def __init__(self, animal):
            self.animal = animal
    
        def __len__(self):
            return len(self.animal)
    
    animals = Animal(['dog', 'cat', 'fish'])
    
    print(len(animals))

      4. __call__ 对实例化的类,传入数据进行执行时,将进入到这个函数里面进行执行 

    class Animal(object):
    
        def __init__(self, animal):
            self.animal = animal
    
        def __call__(self, parse, is_train):
            if is_train:
                print(parse)
            else:
                print('not train')
    
    animals = Animal(['dog', 'cat', 'fish'])
    
    animals(1, False)

       

  • 相关阅读:
    NOIP1996 第三题
    vijos P1071
    USACO 2.3
    NOIP2006 第二题(change)
    NOIP2006 第二题
    NOIP2005 第三题
    Building Block 动态规划
    砝码问题 Weight
    装箱问题(Packing DP)
    算法第二章上机实践报告
  • 原文地址:https://www.cnblogs.com/my-love-is-python/p/12751064.html
Copyright © 2020-2023  润新知