text: # -*- coding: UTF-8 -*-
  from array import array
  import math
  
  
  class Vector2d:
      # 类属性:在实例和字节序列之间转换时使用
      typecode = 'd'
  
      def __init__(self, x, y):
          self.x = float(x)
          self.y = float(y)
  
      @classmethod
      def frombytes(cls, octets):
          # 从第一个字节读取typecode
          typecode = chr(octets[0])
          memv = memoryview(octets[1:]).cast(typecode)
          return cls(*memv)
  
      def __iter__(self):
          """
          定义成可迭代对象,才可以进行拆包
          :return:
          """
          return (i for i in (self.x, self.y))
  
      def __repr__(self):
          class_name = type(self).__name__
          # 此处*self:因实例是可迭代对象的原因可以进行拆包
          return '{}({!r}, {!r})'.format(class_name, *self)
  
      def __str__(self):
          return str(tuple(self))
  
      def __format__(self, format_spec=''):
          if format_spec.endswith('p'):
              format_spec = format_spec[:-1]
              coords = (abs(self), self.angle())
              outer_fmt = '<{}, {}>'
          else:
              coords = self
              outer_fmt = '({}, {})'
          # 将format_spec应用到向量的各个分量上,构建一个可迭代的格式化字符串
          components = (format(c, format_spec) for c in coords)
          return outer_fmt.format(*components)
  
      def __bytes__(self):
          return (bytes([ord(self.typecode)]) +
                  bytes(array(self.typecode, self)))
  
      def __eq__(self, other):
          return tuple(self) == tuple(other)
  
      def __abs__(self):
          return math.hypot(self.x, self.y)
  
      def angle(self):
          return math.atan2(self.y, self.x)
  
      def __bool__(self):
          return bool(abs(self))
  