[Python] Receive print content of another class at execution time not after the end of the...

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    What I have tried:

    import import_ipynb
    import time

    class A:
    def func1(self):
    for i in range(0, 5):
    print(i)
    time.sleep(1)

    class B:
    print('Time is:', A().func1())


    In class B I want to get print content in class A and add 'Time is:' to the beginning and print it in console. One solution is to return i as a list at the end of function, but I want i as instantaneous, not when the function is fully executed. The code I wrote does not do this correctly.
    The output I want is:

    Time is: 0
    Time is: 1
    Time is: 2
    Time is: 3
    Time is: 4

    Continue reading...

Compartilhe esta Página