python查缺补漏--1class中特殊成员

此博客为有关这个视频的p18相关笔记Python大神级教程-8天学会Python 3.9高强度魔鬼训练营从入门到精通(完整版)

特殊成员

在Python的类中存在一些特殊的方法,这些方法都是 __方法__ 格式,这种方法在内部均有特殊的含义,接下来我们来讲一些常见的特殊成员:

  • __init__,初始化方法

    class Foo(object):
        def __init__(self, name):
            self.name = name
    
    
    obj = Foo("武沛齐")
    
  • __new__,构造方法

    class Foo(object):
        def __init__(self, name):
            print("第二步:初始化对象,在空对象中创建数据")
            self.name = name
    
        def __new__(cls, *args, **kwargs):
            print("第一步:先创建空对象并返回")
            return object.__new__(cls)
    
    
    obj = Foo("武沛齐")
    
  • __call__

    class Foo(object):
        def __call__(self, *args, **kwargs):
            print("执行call方法")
    
    
    obj = Foo()
    obj()
    
  • __str__

    class Foo(object):
    
        def __str__(self):
            return "哈哈哈哈"
    
    
    obj = Foo()
    data = str(obj)
    print(data)
    
  • __dict__

    class Foo(object):
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
    
    obj = Foo("武沛齐",19)
    print(obj.__dict__)
    
  • __getitem____setitem____delitem__

    class Foo(object):
    
        def __getitem__(self, item):
            pass
    
        def __setitem__(self, key, value):
            pass
    
        def __delitem__(self, key):
            pass
    
    
    obj = Foo("武沛齐", 19)
    
    obj["x1"]
    obj['x2'] = 123
    del obj['x3']
    
  • __enter____exit__

    class Foo(object):
    
        def __enter__(self):
            print("进入了")
            return 666
    
        def __exit__(self, exc_type, exc_val, exc_tb):
            print("出去了")
    
    
    obj = Foo()
    with obj as data:
        print(data)
    
    超前知识:数据连接,每次对远程的数据进行操作时候都必须经历。
    1.连接 = 连接数据库
    2.操作数据库
    3.关闭连接
    
    class SqlHelper(object):
    
        def __enter__(self):
            self.连接 = 连接数据库
            return 连接
    
        def __exit__(self, exc_type, exc_val, exc_tb):
            self.连接.关闭
    
            
            
    with SqlHelper() as 连接:
        连接.操作..
        
        
    with SqlHelper() as 连接:
        连接.操作...
    
    # 面试题(补充代码,实现如下功能)
    
    class Context:
        
        def do_something(self):
            print('内部执行')
    
    
    with Context() as ctx:
        print('内部执行')
        ctx.do_something()
    
    

    上下文管理的语法。

  • __add__ 等。

    class Foo(object):
        def __init__(self, name):
            self.name = name
    
        def __add__(self, other):
            return "{}-{}".format(self.name, other.name)
    
    
    v1 = Foo("alex")
    v2 = Foo("sb")
    
    # 对象+值,内部会去执行 对象.__add__方法,并将+后面的值当做参数传递过去。
    v3 = v1 + v2
    print(v3)
    
  • __iter__

    • 迭代器

      # 迭代器类型的定义:
          1.当类中定义了 __iter__ 和 __next__ 两个方法。
          2.__iter__ 方法需要返回对象本身,即:self
          3. __next__ 方法,返回下一个数据,如果没有数据了,则需要抛出一个StopIteration的异常。
      	官方文档:https://docs.python.org/3/library/stdtypes.html#iterator-types
              
      # 创建 迭代器类型 :
      	class IT(object):
              def __init__(self):
                  self.counter = 0
      
              def __iter__(self):
                  return self
      
              def __next__(self):
                  self.counter += 1
                  if self.counter == 3:
                      raise StopIteration()
                  return self.counter
      
      # 根据类实例化创建一个迭代器对象:
          obj1 = IT()
          
          # v1 = obj1.__next__()
          # v2 = obj1.__next__()
          # v3 = obj1.__next__() # 抛出异常
          
          v1 = next(obj1) # obj1.__next__()
          print(v1)
      
          v2 = next(obj1)
          print(v2)
      
          v3 = next(obj1)
          print(v3)
      
      
          obj2 = IT()
          for item in obj2:  # 首先会执行迭代器对象的__iter__方法并获取返回值,一直去反复的执行 next(对象) 
              print(item)
              
      迭代器对象支持通过next取值,如果取值结束则自动抛出StopIteration。
      for循环内部在循环时,先执行__iter__方法,获取一个迭代器对象,然后不断执行的next取值(有异常StopIteration则终止循环)。
      
    • 生成器

      # 创建生成器函数
          def func():
              yield 1
              yield 2
          
      # 创建生成器对象(内部是根据生成器类generator创建的对象),生成器类的内部也声明了:__iter__、__next__ 方法。
          obj1 = func()
          
          v1 = next(obj1)
          print(v1)
      
          v2 = next(obj1)
          print(v2)
      
          v3 = next(obj1)
          print(v3)
      
      
          obj2 = func()
          for item in obj2:
              print(item)
      
      如果按照迭代器的规定来看,其实生成器类也是一种特殊的迭代器类(生成器也是一个中特殊的迭代器)。
      
    • 可迭代对象

      # 如果一个类中有__iter__方法且返回一个迭代器对象 ;则我们称以这个类创建的对象为可迭代对象。
      
      class Foo(object):
          
          def __iter__(self):
              return 迭代器对象(生成器对象)
          
      obj = Foo() # obj是 可迭代对象。
      
      # 可迭代对象是可以使用for来进行循环,在循环的内部其实是先执行 __iter__ 方法,获取其迭代器对象,然后再在内部执行这个迭代器对象的next功能,逐步取值。
      for item in obj:
          pass
      
      class IT(object):
          def __init__(self):
              self.counter = 0
      
          def __iter__(self):
              return self
      
          def __next__(self):
              self.counter += 1
              if self.counter == 3:
                  raise StopIteration()
              return self.counter
      
      
      class Foo(object):
          def __iter__(self):
              return IT()
      
      
      obj = Foo() # 可迭代对象
      
      
      for item in obj: # 循环可迭代对象时,内部先执行obj.__iter__并获取迭代器对象;不断地执行迭代器对象的next方法。
          print(item)
      
      # 基于可迭代对象&迭代器实现:自定义range
      class IterRange(object):
          def __init__(self, num):
              self.num = num
              self.counter = -1
      
          def __iter__(self):
              return self
      
          def __next__(self):
              self.counter += 1
              if self.counter == self.num:
                  raise StopIteration()
              return self.counter
      
      
      class Xrange(object):
          def __init__(self, max_num):
              self.max_num = max_num
      
          def __iter__(self):
              return IterRange(self.max_num)
      
      
      obj = Xrange(100)
      
      for item in obj:
          print(item)
      
      class Foo(object):
          def __iter__(self):
              yield 1
              yield 2
      
      
      obj = Foo()
      for item in obj:
          print(item)
      
      # 基于可迭代对象&生成器 实现:自定义range
      
      class Xrange(object):
          def __init__(self, max_num):
              self.max_num = max_num
      
          def __iter__(self):
              counter = 0
              while counter < self.max_num:
                  yield counter
                  counter += 1
      
      
      obj = Xrange(100)
      for item in obj:
          print(item)
      

      常见的数据类型:

      v1 = list([11,22,33,44])
      
      v1是一个可迭代对象,因为在列表中声明了一个 __iter__ 方法并且返回一个迭代器对象。
      
      from collections.abc import Iterator, Iterable
      
      v1 = [11, 22, 33]
      print( isinstance(v1, Iterator) )  # false,判断是否是迭代器;判断依据是__iter__ 和 __next__。
      v2 = v1.__iter__()
      print( isinstance(v2, Iterator) )  # True
      
      
      
      v1 = [11, 22, 33]
      print( isinstance(v1, Iterable) )  # True,判断依据是是否有 __iter__且返回迭代器对象。
      
      v2 = v1.__iter__()
      print( isinstance(v2, Iterable) )  # True,判断依据是是否有 __iter__且返回迭代器对象。
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值