Is passing 'self' to another class possible

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
mc2software
Posts: 22
Joined: Wed Jan 13, 2021 6:08 pm
Location: Northern Florida

Is passing 'self' to another class possible

Post by mc2software » Sat Apr 17, 2021 8:30 pm

I have a class and I want to pass the class as an argument to a function in another class. I'm thinking I can't do this because I have not instantiated an object of either class.

Code: Select all

# constants
PLAYER1 = 0
PLAYER2 = 1
RACKS = 2
BALLSREMAINING = 3
ADD = 1
SUBTRACT = 2
INITALIZE = 0
LOW = 0
HIGH = 1

class Score:
    def __init__(self, display, buttonLeds, numDisplays, numDigits):

        """
        display is the object that will display the calculations
        Constructor:
        """
        self.numPlayers = 2
        self.numButtons = self.numPlayers * 2
        self.playerScore = []
        self.playerUp = 0
        self.numRacks = 0
        self.ballsOnTable = 15
        self.ballsOnFoul = 0
        self.buttonLeds = buttonLeds
        self.ledDisplay = [numDisplays]
        self.ledDigit = [numDigits]
        self.decimalPoint = False
        self.quickStart = False

        for i in range(self.numPlayers):
            self.playerScore.append(0)
            
    def updateDisplay(self, display):
        if isinstance(mc2Display, mc2Max7219.SevenSegment):
            LedDisplay.updateDisplay(self, display)
        else:
            LcdDisplay.updateDisplay(self, display)

In the above class I want to call a function in the following class:

Code: Select all

class LedDisplay:
    def __init__(self):

        """
        This handles the display of the score. We keep them separate so that, if we move to LCD from LED display,
            the Display and Score classes can potentially be the same or just swapped

        """
        self.quickStart = False
        self.displayText = '000000 00 00'   #default display pattern

    def updateDisplay(self, display, score):
        #display is a mc2Max7219 object

        numDisplays = 4

        digits = []
        for i in range(numDisplays):
            digits.append(' ')

        digits[0] = "{:03d}".format(score.playerScore[0])
        digits[1] = "{:03d}".format(score.playerScore[1])
        if self.ballsOnTable < 0:
            digits[2] = "{:03d}".format(score.ballsOnTable)
        else:
            digits[2] = " {:02d}".format(score.ballsOnTable)
        if self.numRacks < 10:
            digits[3] = "  {:01d}".format(score.numRacks)
        else:
            digits[3] = " {:02d}".format(score.numRacks)

        display.displayText(digits[0] + digits[1] + digits[2] + digits[3])
I'm trying to separate the programming logic from the UI. As I write this post, I'm thinking I need to do this from yet another class or in the main.py.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Is passing 'self' to another class possible

Post by pythoncoder » Sun Apr 18, 2021 12:26 pm

Most applications involving displays instantiate the display early in the code's execution. That instance - typically a global singleton - is then available for use by any other object.
Peter Hinch
Index to my micropython libraries.

Post Reply