python特殊函数__len__

  • ​ Python特殊函数__len()__使函数len()能够作用于类中的变量,例:

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      class (object):

      def __init__(self, *args):
      self.names = args

      def __len__(self):
      return len(self.names)

      s = Namespace('jia', 'tim')
      print( len(s) )