Menu

Subclassing Python Classes not working for some reason

Help
2019-12-08
2019-12-16
  • Flower lady

    Flower lady - 2019-12-08

    Hi folks,

    I'm getting a strange bug. I initialize a base class:

    """
    Script for the BaseBehavior class
    """
    from PythonQt.scriptedBehaviors import PythonBehavior
    
    # ==============================================================================
    # Globals
    # ==============================================================================
    DEBUG_MODE = True
    
    # ==============================================================================
    # Classes
    # ==============================================================================
    
    class BaseBehavior():
        """ Base behavior from which all scripted behaviors must inherit
        """
        def __init__(self):
            """Create the behavior, and underlying C++ PythonBehavior object"""
            self._behavior = PythonBehavior()
            self.log_info("CONSTRUCTED PYTHON BEHAVIOR")
            return
    
        def initialize(self):
            """Initialize"""
            self.log_info("INITIALIZED BEHAVIOR")
            return 1
    
        def update(self, delta_ms: int):
            self.log_info("UPDATING BEHAVIOR")
            return 1
    
        def fixed_update(self, delta_ms: int):
            self.log_info("FIXED UPDATE FOR BEHAVIOR")
            return 1
    
        def log_info(self, info: str=""):
            """ Log given string as info """
            if DEBUG_MODE:
                self._behavior.log_info(info)
                return 1
            return 0
    
        def log_warning(self, info: str=""):
            """ Log given string as info """
            self._behavior.log_warning(info)
            return 1
    

    Using:

    PythonQt::self()->getMainModule().evalScript(code);
    

    My base class logs messages to std::out, and this works just fine when I instantiate the base class:

    test_base = BaseBehavior()
    

    However, I tried subclassing BaseBehavior as such:

    """
    Script for the TestBehavior class
    """
    from base_behavior import BaseBehavior 
    # from PythonQt.components import Transform
    # ==============================================================================
    # Globals
    # ==============================================================================
    
    # ==============================================================================
    # Classes
    # ==============================================================================
    class TestBehavior(BaseBehavior):
        """ Test behavior from which all scripted behaviors must inherit
        """
        def __init__(self):
            """Create the behavior, and underlying C++ PythonBehavior object"""
            # BaseBehavior.__init__(self)
            super().__init__()
            return
    
        def initialize(self):
            """Initialize"""
            return True
    
        def update(self, delta_ms: int):
            return super().update(delta_ms)
    
        def fixed_update(self, delta_ms: int):
            return True
    

    And then instantiated the subclass:

    test_subclass = TestBehavior()
    

    But no messages get logged. Very strange.. They DO get logged when I replace my update method with:

        def update(self, delta_ms: int):
            self._behavior.log_info("UPDATING BEHAVIOR")
    

    This means that the parent class constructor is getting called properly, but that for some reason, the parent class's update routine isn't getting called correctly. Additionally, when I try to call log_info from the parent class as below, I get the same failure to log messages.

        def update(self, delta_ms: int):
            return super().log_info("UPDATING BEHAVIOR")
    

    Any ideas? If it helps, when I try to use PythonQt's "call" on the update routine to return "super().update(delta_ms)", I get an invalid QVariant returned, instead of the actual output.

     
  • Florian Link

    Florian Link - 2019-12-08

    Google for super and its problems. Then do direct calls to your base class using BaseClass.xzy(self, arg1, ...). Btw, this has nothing to do with PythonQt itself, it is a pure Python problem.

     
  • Flower lady

    Flower lady - 2019-12-08

    Okay, I just thought this might be PythonQt-specific, since it runs fine when I run these scripts directly with the same python distribution. Calling the base class directly, as below, also doesn't seem to help me:

    def update(self, delta_ms: int):
    """Update routine for TestBehavior"""
            return BaseBehavior.update(self, delta_ms)
    
     
  • Flower lady

    Flower lady - 2019-12-16

    Resolved this, the problem was some incorrect import syntax as I was pulling everything into the main module scope.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.