Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. What does the "yield" keyword do?
Forum Update on Monday, May 27th 2025

What does the "yield" keyword do?

Scheduled Pinned Locked Moved Unsolved Qt for Python
python
3 Posts 2 Posters 489 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aliviya
    wrote on last edited by JKSH
    #1

    What is the use of the yield keyword in the Python framework? What does it do? [EDIT: Link removed --JKSH]

    For example, I'm trying to understand this code

    def _get_child_candidates(self, distance, min_dist, max_dist):
        if self._leftchild and distance - max_dist < self._median:
            yield self._leftchild
        if self._rightchild and distance + max_dist >= self._median:
            yield self._rightchild  
    
    

    And this is the caller:

    result, candidates = [], [self]
    while candidates:
        node = candidates.pop()
        distance = node._get_dist(obj)
        if distance <= max_dist and distance >= min_dist:
            result.extend(node._values)
        candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))
    return result
    

    What happens when the method _get_child_candidates is called? Is a list returned? A single element? Is it called again? When will subsequent calls stop?

    jsulmJ 1 Reply Last reply
    0
    • A Aliviya

      What is the use of the yield keyword in the Python framework? What does it do? [EDIT: Link removed --JKSH]

      For example, I'm trying to understand this code

      def _get_child_candidates(self, distance, min_dist, max_dist):
          if self._leftchild and distance - max_dist < self._median:
              yield self._leftchild
          if self._rightchild and distance + max_dist >= self._median:
              yield self._rightchild  
      
      

      And this is the caller:

      result, candidates = [], [self]
      while candidates:
          node = candidates.pop()
          distance = node._get_dist(obj)
          if distance <= max_dist and distance >= min_dist:
              result.extend(node._values)
          candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))
      return result
      

      What happens when the method _get_child_candidates is called? Is a list returned? A single element? Is it called again? When will subsequent calls stop?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Aliviya said in What does the "yield" keyword do?:

      yield

      returns a gerator object which can be iterated to get the values.
      See https://www.guru99.com/python-yield-return-generator.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Aliviya said in What does the "yield" keyword do?:

        yield

        returns a gerator object which can be iterated to get the values.
        See https://www.guru99.com/python-yield-return-generator.html

        A Offline
        A Offline
        Aliviya
        wrote on last edited by
        #3

        @jsulm said in What does the "yield" keyword do?:

        returns a gerator object which can be iterated to get the values.
        See https://www.guru99.com/python-yield-return-generator.html

        Thank you..It has a great overview of how yield works and how to use it.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved