line_profiler: what am I doing wrong?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Feb 10 19:27:01 EST 2015


Neal Becker wrote:

> I inserted
> @profile
> def run(...)
> 
> into a module-level global function called 'run'.  Something is very wrong
> here. 1. profile results were written before anything even ran
> 2. profile is not defined?

Well, is it defined? Where does it come from?

If you defined it yourself, it needs to be defined before you can use it.
This won't work:


@profile
def run(...)

def profile(func): ...


Swap the order of profile and run and it should work. (Give or take any
additional bugs in your code.)


If you've imported it from an external module, how did you import it?


import some_module

@some_module.profile
def run(...)


should work. So will this:


from some_module import profile

@profile
def run(...)


But this won't:


import some_module

@profile
def run(...)


and will fail with NameError, exactly as you are experiencing.




-- 
Steven




More information about the Python-list mailing list