0

Simple question – can I print the source code of this if?

if __name__ == '__main__':
  # ...
  # this is the `if` I want to print
  #  V 
  if args and args[0][0] == '-':
    if args[0] in ('--help','-h','-?'):
      print_source_of_block(level=2)
    icase = 'i' in args[0]
    desc = 'd' in args[0]
    args = args[1:]

Please don't ask me why I need it, nor give me advice on how to write user help. I want to know it because I'm curious.

5
  • 1
    I don't ask why you need this but here are some essential questions. You have this snippet in a file or a function? Is it part of an object or not? have you searched about it so far? what have you tried and whats the problem with your code? Commented Mar 29, 2017 at 5:54
  • Sorry to be so rude, but I haven't done any research and I don't plan on doing any. Neither do I expect anyone to do research for me. Rather I just ask in case someone knows the answer. If no one knows, it's not a problem. It's not vital for me to know the answer to this question and neither do I have time to look for an answer on my own in other way than simply asking on stackoverflow. In other cases I would take an effort, but I suspect this will not even be possible and hence I don't want to waste time on it. Commented Mar 29, 2017 at 5:58
  • Well that's ok, but you need to be clear enough in your question if you expect a correct and comprehensive answer and that's why my first question was about your code. Also I think searching in google and just reading would be so faster than asking a question and waiting for the sun to shine ;-). Nevertheless, if you just want to know if it's possible or not the answer is yes. Because based on Von Neumann’s model of a “stored program computer,” code is also represented by objects. And accessing to objects in python is usually so easy. Commented Mar 29, 2017 at 6:15
  • You're implying it's "so easy" to do what I want? Then show me :D Commented Mar 29, 2017 at 6:18
  • It's like giving someone a fish or teaching him/her the fishing. Unfortunately I'm not the one who gives you the fish, maybe if your question was a trivial one I'd do that but this is an interesting question and doesn't come in everybody's mind easily, and that's why I think the questioner deserves more than just a simple answer. Commented Mar 29, 2017 at 6:26

2 Answers 2

2

Might want to look at the inspect module (https://docs.python.org/2/library/inspect.html). Specifically, the inspect.getsource(object) method.

Sign up to request clarification or add additional context in comments.

2 Comments

I figure I could use a hack based on Python's indentation-defined block syntax. No actual parsing necessary.
@kyrill: That's almost what the tokenization-based solution would be, except that you can also have things like one-liner ifs, which do not involve INDENT and DEDENT tokens in the token stream. It's much harder to get those right, hard enough to make the AST solution preferable.
1

Yeah, that's doable. Here's a quick sketch of how you could do it and what tools would be involved:

  1. Retrieve the stack frame of print_source_of_block's caller with inspect.currentframe().f_back.
  2. Determine the caller's source file and line number within that source file with inspect.getframeinfo.
  3. Open the source file.
  4. Either
  5. Using either the abstract syntax tree or the token stream, determine where the requested source block begins and ends.
  6. Print the block.

1 Comment

I kind of hoped I could avoid steps 4 and 5, but I guess that wasn't a very realistic expectation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.