-3

I'd like to automatically print python statements as they are called in a script while the script runs. Could anybody show a ready-to-use solution?

For example, I may have the following python code.

print "xxx"
myfun()

I'd like print "xxx" and myfun() be printed exactly in the output.

One solution is manually to add a print statement to each function. But this is combersome.

print 'print "xxx"'
print "xxx"
print 'myfun()'
myfun()

The following solutions just can not do so. Please do not mark this question as duplicate.

The solution here will not only print the functions in the current script but also functions in other scripts used in the current scripts. These solutions are not what I need.

How do I print functions as they are called

The following does not do what I need because it does not limit the decoration to the current file. For example, it uses import mymod1; decorate_all_in_module(mymod1, decorator). I don't have a module. I want to print the statements in the current script.

How can I decorate all functions imported from a file?

user1424739
  • 11,937
  • 17
  • 63
  • 152
  • 1
    I've asked this the last time you (re-)posted this question, but what exactly does _"[...] it does not limit the decoration to the current file"_ mean? Is it a problem that it also decorates imported functions? – Aran-Fey Mar 11 '18 at 14:48
  • @Aran-Fey See my edit. – user1424739 Mar 11 '18 at 14:50
  • 1
    _"I don't have a module."_ Yes you do - it's called `__main__`. Try `import __main__; decorate_all_in_module(__main__, decorator)`. – Aran-Fey Mar 11 '18 at 14:50
  • You need to **edit** your posts when closed as a duplicate. Deleting and reposting is only going to lead to more downvotes and closures, which in turn can lead to a post ban. I did try to tell you the previous time you did this. As pointed out, you do have a module, called `__main__`. You can also use `globals()` (`for name in globals():` and `globals()[name]` to get and `globals()[name] = decorator(obj)` to assign) – Martijn Pieters Mar 11 '18 at 18:02
  • @MartijnPieters So this just proved that question was not a duplicate. – user1424739 Mar 11 '18 at 19:02
  • @user1424739: yet you can use `__main__` as the module, and the other solution applies. You have not really shown much research, and I am mostly irked by your behaviour of deleting and re-posting rather than editing. – Martijn Pieters Mar 11 '18 at 19:15
  • @MartijnPieters My original question was not clearly written. It should not viewed as being connected to the current version in anyway. That is why the original version should be deleted. – user1424739 Mar 11 '18 at 19:22
  • @user1424739 You'll have to explain _why_ you think that this isn't a duplicate. Martijn, I, and a few other smart people all agree that it _is_ a duplicate. If you have a reason to believe that it's not, you'll have to try to convince us otherwise - but you'll have to provide some arguments and explanation, not just the sentence "This isn't a duplicate". – Aran-Fey Mar 12 '18 at 07:27
  • @user1424739 The statement that the original question should be deleted because it was poorly written is just ridiculous. Rephrasing a poorly written question is precisely what the [edit] feature is for. You didn't even make any significant changes to your question; you basically reposted the same thing 3 times. – Aran-Fey Mar 12 '18 at 07:31
  • @Aran-Fey If you think my question is a duplicate, please answer this question. How to print `'print "xxx"`' automatically in my example using these so-called duplicated solutions? If you can not, then it is not a duplicate. You are just rediculous. – user1424739 Mar 12 '18 at 20:08
  • @user1424739 Ah you're right, it doesn't address that aspect of your question. Not sure in which version you sneaked that in. I kind of started to skim your question after you reposted it so many times. So, to answer that last bit of your question: It's not possible. There's no way around manually adding `print` statements. So in the end it's still reasonable to close it as a duplicate. There's really no reason to post a new answer that states "You have to manually add `print` statements, but for everything else you can use ". – Aran-Fey Mar 12 '18 at 21:46

1 Answers1

-1

There is no "ready-to-use" solution to this problem. You are asking for a programming language with different semantics than the Python language: therefore you will either need to write code to get this effect, or use a different language, or both.

That said, for solving your actual problem, I believe simply using a debugger would do what you want: that way you can step over each statement in your program and observe as they are executed.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • There is a solution, and we've given the various options twice already. The OP has just not been able to see how they can be applied. – Martijn Pieters Mar 11 '18 at 18:25
  • @MartijnPieters has never given a solution that exactly answered my question. – user1424739 Mar 11 '18 at 19:04
  • @user1424739; that doesn't mean there is no solution to the problem. This answer is simply wrong, not what you asked for and not helpful. – Martijn Pieters Mar 11 '18 at 19:14
  • @MartijnPieters No questions are wrong. If you think there is a solution, then provide it. @DanielPryden said there is not a solution. I also don't think there is a way to print `print "xxx"`. It is just wrong to mark a question that is not a duplicate as a duplicate. – user1424739 Mar 11 '18 at 19:21