mercredi 19 avril 2017

Python warnings come after thing trying to warn user about

I am using warnings in object code to alert users that something has happened but not halt the code. Below is a simple mockup based on the more complex scenarios I am encountering in my real code:

class myClass(object):        
    def __init__(self, numberArg):

        if numberArg > 1000:
            self._tmpTxt = "That's a really big number for this code." + \ 
                           "Code may take a while to run..."
            warn("\n%s %s" %(numberArg, self._tmpTxt))
            # begin processing and here ...
            # code does more stuff here ...

In my real code, when I instantiate the class which executes __init__(self, numberArg), the warning is output at the end after all the processing that follows the warning is done. Why does this happen?

More importantly, is there a way to ensure the warning is output first and then the rest of my code runs and delivers its output?

As with the example provided here, the desired effect is to alert the user what is going to happen before it happens, not afterwards, and deliver the output like a warning with warning format.

Note: This problem was encountered using Python 2.7 on iPython/Jupyter


0 commentaires:

Enregistrer un commentaire