how to detect comment in source code file ?

Steven D'Aprano steve at pearwood.info
Wed Sep 4 21:56:49 EDT 2013


On Wed, 04 Sep 2013 17:02:42 +1000, Ben Finney wrote:

> vnkumbhani at gmail.com writes:
> 
>> how works python interpreter for finding comment ?
> 
> It works as specified in the language reference. In particular, see
> <URL:http://docs.python.org/3/reference/lexical_analysis.html#comments>.
> 
>> if possible find nested comment ?
> 
> Python's comments are line-end comments only. The syntax does not allow
> multi-line nor nested comments.

While that's technically true, you can use bare strings as de facto 
comments. The compiler drops any bare strings it sees, so one can nest 
pseudo-comments like this:


do_this()
"""
do_that()
do_something()  # Make the widget work correctly.
'''
for x in range(5):
    do_something_else()
'''
do_something_different()
"""


In the above, everything except do_this() is commented out by being 
turned into a string.


-- 
Steven



More information about the Python-list mailing list