[Python] Repeatedly calling a function with a string arg: operating on the string returns the...

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    I'm repeatedly calling a function that takes a string as an argument and operates on it, however every time the function is called, the passed string argument takes the value of the argument the first time the function was called only when it is operated on.

    Just explaining some structure, it starts with this code:

    # message handler for connected clients
    async def message_handler(websocket):

    try:
    # this block is where message recieving and processing happens
    async for message in websocket:

    normData = footsiesAI.prepareData(message)


    This is a Python websocket server that receives message from a Unity game hosting a C# websocket client. Messages are received at a rate of 60 messages a second.

    footsiesAI is an object of the following class:

    class FootsiesPredictor():
    def __init__(self, modelPath, sequenceLength, features):
    self.model = load_model(modelPath)
    self.seqL = sequenceLength
    self.features = features
    self.buffer = []
    self.myNormaliser = Normaliser()


    def prepareData(self, rawLine):
    _, parsedLine = parse_line(rawLine)
    normalisedFrame = self.myNormaliser.normaliseLine(parsedLine)
    normalisedFrame = normalisedFrame.drop(["round_ID", "frame_number"], errors="ignore")
    return normalisedFrame


    self.myNormaliser and anything beyond _, parsedLine = parse_line(rawLine) can be ignored temporarily.

    This is the function parse_line():

    def parse_line(rawLine):

    print(rawLine)
    print(rawLine[rawLine.index("P1_INFO:"):rawLine.index("P2_INFO:")])


    Obviously this is not what the code actually does, but this same error applies.

    The first print statement correctly prints out whatever the value of the passed rawLine is, however the second print statement prints out the result of the expression using the value for rawLine the first time it was passed to the function. So rawLine is updating, but not when its operated on.

    Continue reading...

Compartilhe esta Página