method overloading in micropython



  • Hello,

    As far as I know there is know method overloading in python or micropython
    Whats the best practice workaround for that?
    I created class for the rgbled. I want to use that for notify me about statuses, and progresses.
    It would be nice to run these notification on second thread, because it's operate with sleeps, and don't want to sleep the whole system for blinking an led.
    BTW, any feedback for my code is welcome ! :)

    My code:

    import pycom
    import time
    
    
        ##################################
        ######## avaible colors ##########
        ##################################
            # 0xFF3300 - narancs - orange 
            # 0xFF0000 - piros - red
            # 0x0000FF - kék - blue
            # 0xEE00FF - lila - purple
            # 0xFFFFFF - fehér - white
            # 0xFF0088 - rózsaszín - pink
            # 0x00FF00 - zöld - green
            # 0x00FFFF - cián - cyan
            # 0xFFBB00 - sárga - yellow
    
        ###################################
        ######## avaible methods ##########
        ###################################
    
    		# longsignal(self)
    		# longsignal(self, color)
    		# criticalError(self)
    		# message(self, color)
    		# message(self, color, time)
    		# message(self, color, time, iteration)
    		# okay(self)
    		# startup(self)
    		# shutdown(self)
    		# SOS(self, color)
    
    
    class RGBSignal:
    
        colors = {
            'orange' : 0xFF3300,
            'red' : 0xFF0000,
            'blue' : 0x0000FF,
            'purple' : 0xEE00FF,
            'white' : 0xFFFFFF,
            'pink' : 0xFF0088,
            'green' : 0x00FF00,
            'cyan' : 0x00FFFF,
            'yellow' : 0xFFBB00,
            'off': 0x000000
        }
    
    
        def  __init__(self): #this is how a ctor look like
           pycom.heartbeat(False) 
    
        def startup(self):
    		for k in self.colors:
    			pycom.rgbled(self.colors[k])
    			time.sleep(0.1)
    			pycom.rgbled(self.colors['off'])
    			time.sleep(0.1)
    
        def longsignal(self):
            pycom.rgbled(self.colors['white'])
            time.sleep(1)
            pycom.rgbled(self.colors['off'])
        
        def longsignal(self, color):
            pycom.rgbled(self.colors[color])
            time.sleep(1)
            pycom.rgbled(self.colors['off'])
        
        def criticalError(self):
            for x in range(50):
                pycom.rgbled(self.colors['red'])
                time.sleep(0.05)
                pycom.rgbled(self.colors['off'])
                time.sleep(0.05)
    
        def message(self, color, time, iteration):
            for x in range(iteration):
                pycom.rgbled(self.colors[color])
                time.sleep(time)
                pycom.rgbled(self.colors['off'])
                time.sleep(time)
        
        
        def message(self, color):
                pycom.rgbled(self.colors[color])
                time.sleep(0.5)
                pycom.rgbled(self.colors['off'])
    
        def message(self, color, time):
                pycom.rgbled(self.colors[color])
                time.sleep(time)
                pycom.rgbled(self.colors['off'])
    
        def okay(self):
                pycom.rgbled(self.colors['green'])
                time.sleep(0.6)
                pycom.rgbled(self.colors['off'])
    
        def SOS(self, color):
            pycom.rgbled(self.colors[color])
            time.sleep(0.1)
            pycom.rgbled(self.colors['off'])
            time.sleep(0.1)
            pycom.rgbled(self.colors[color])
            time.sleep(0.1)
            pycom.rgbled(self.colors['off'])
            time.sleep(0.1)
            pycom.rgbled(self.colors[color])
            time.sleep(0.1)
            pycom.rgbled(self.colors['off'])
            time.sleep(0.1)
            pycom.rgbled(self.colors[color])
            time.sleep(0.2)
            pycom.rgbled(self.colors['off'])
            time.sleep(0.2)
            pycom.rgbled(self.colors[color])
            time.sleep(0.2)
            pycom.rgbled(self.colors['off'])
            time.sleep(0.2)
            pycom.rgbled(self.colors[color])
            time.sleep(0.2)
            pycom.rgbled(self.colors['off'])
            time.sleep(0.2)
            pycom.rgbled(self.colors[color])
            time.sleep(0.1)
            pycom.rgbled(self.colors['off'])
            time.sleep(0.1)
            pycom.rgbled(self.colors[color])
            time.sleep(0.1)
            pycom.rgbled(self.colors['off'])
            time.sleep(0.1)
            pycom.rgbled(self.colors[color])
            time.sleep(0.1)
            pycom.rgbled(self.colors['off'])
            time.sleep(0.1)
    
        def shutdown(self):
            for x in range(10):
                pycom.rgbled(self.colors['red'])
                time.sleep(0.1)
                pycom.rgbled(self.colors['off'])
                time.sleep(0.1)
                pycom.rgbled(self.colors['green'])
                time.sleep(0.1)
                pycom.rgbled(self.colors['off'])
    


  • Thank you for the detailed answer!
    This is the best solution what I could find.



  • Use default values for arguments.

    def message(self, color, time=0.5, iteration=1):
        for x in range(iteration):
            pycom.rgbled(self.colors[color])
            time.sleep(time)
            pycom.rgbled(self.colors['off'])
            time.sleep(time)
    

    This method can be called as:

    message('orange')
    
    message('orange', time=1.4)
    message('orange', 1.4)
    
    message('orange', time=1.4, iteration=6)
    message('orange', 1.4, 6)
    
    message('orange', iteration=6)  # which will use the default value 0.5 for time
    

    Note that this is not identical to your message(self, color) and message(self, color, time) methods because they do not have a time.sleep(time) after the LED is turned off. If that is necessary, use:

    def message(self, color, time=0.5, iteration=1):
        for x in range(iteration):
            pycom.rgbled(self.colors[color])
            time.sleep(time)
            pycom.rgbled(self.colors['off'])
            
            if iteration > 1:
                time.sleep(time)
    

    This Stack Overflow question has some other solutions
    https://stackoverflow.com/questions/6434482/python-function-overloading



Pycom on Twitter