code: def subscribe(self, topics=(), pattern=None, listener=None):
          
          if self._user_assignment or (topics and pattern):
              raise IllegalStateError(self._SUBSCRIPTION_EXCEPTION_MESSAGE)
          assert topics or pattern, 'Must provide topics or pattern'
  
          if pattern:
              log.info('Subscribing to pattern: /%s/', pattern)
              self.subscription = set()
              self.subscribed_pattern = re.compile(pattern)
          else:
              self.change_subscription(topics)
  
          if listener and not isinstance(listener, ConsumerRebalanceListener):
              raise TypeError('listener must be a ConsumerRebalanceListener')
          self.listener = listener
docs: Subscribe to a list of topics, or a topic regex pattern.
  
          Partitions will be dynamically assigned via a group coordinator.
          Topic subscriptions are not incremental: this list will replace the
          current assignment (if there is one).
  
          This method is incompatible with assign_from_user()
  
          Arguments:
              topics (list): List of topics for subscription.
              pattern (str): Pattern to match available topics. You must provide
                  either topics or pattern, but not both.
              listener (ConsumerRebalanceListener): Optionally include listener
                  callback, which will be called before and after each rebalance
                  operation.
  
                  As part of group management, the consumer will keep track of the
                  list of consumers that belong to a particular group and will
                  trigger a rebalance operation if one of the following events
                  trigger:
  
                  * Number of partitions change for any of the subscribed topics
                  * Topic is created or deleted
                  * An existing member of the consumer group dies
                  * A new member is added to the consumer group
  
                  When any of these events are triggered, the provided listener
                  will be invoked first to indicate that the consumer's assignment
                  has been revoked, and then again when the new assignment has
                  been received. Note that this listener will immediately override
                  any listener set in a previous call to subscribe. It is
                  guaranteed, however, that the partitions revoked/assigned
                  through this interface are from topics subscribed in this call.