Python and PEP8 - Recommendations on breaking up long lines?

Steven D'Aprano steve at pearwood.info
Wed Nov 27 22:02:10 EST 2013


On Thu, 28 Nov 2013 13:47:22 +1100, Ben Finney wrote:


>> Third example - long comments:
>>
>>     """ NB - We can't use Psycopg2's parametised statements here, as
>>     that automatically wraps everything in single quotes. So
>>     s3://my_bucket/my_file.csv.gz would become
>>     s3://'my_bucket'/'my_file.csv.gz'. Hence, we use Python's normal
>>     string formating - this could potentially exposes us to SQL
>>     injection attacks via the config.yaml file. I'm not aware of any
>>     easy ways around this currently though - I'm open to suggestions
>>     though.
>>     See
>>     http://stackoverflow.com/questions/9354392/psycopg2-cursor-execute-
with-sql-query-parameter-causes-syntax-error
>>     for further information. """
> 
> That's not syntactically a comment, and I don't think pretending
> triple-quoted strings are comments is good practice. If nothing else,
> you'll need a special case if you want to enclose something with
> existing triple-quotes.

The CPython core devs disagree with you. Using bare strings for comments 
is supported by Python, and the compiler strips them out at compile-time:


steve at runes:~$ python3.3
Python 3.3.0rc3 (default, Sep 27 2012, 18:31:58) 
[GCC 4.4.5] on linux
Type "help", "copyright", "credits" or "license" for more information.
py> import dis
py> dis.dis('x = 1; """Comment"""; y = 1')
  1           0 LOAD_CONST               0 (1) 
              3 STORE_NAME               0 (x) 
              6 LOAD_CONST               0 (1) 
              9 STORE_NAME               1 (y) 
             12 LOAD_CONST               1 (None) 
             15 RETURN_VALUE         
py> 


I'm on the fence on this one. I can it's useful; but it's also 
inconsistent with docstrings. And I wonder why other "dead objects" 
aren't also dropped:

py> dis.dis('x = 1; {23: "a", "b": 42}; y = 1')
  1           0 LOAD_CONST               0 (1) 
              3 STORE_NAME               0 (x) 
              6 BUILD_MAP                2 
              9 LOAD_CONST               1 ('a') 
             12 LOAD_CONST               2 (23) 
             15 STORE_MAP            
             16 LOAD_CONST               3 (42) 
             19 LOAD_CONST               4 ('b') 
             22 STORE_MAP            
             23 POP_TOP              
             24 LOAD_CONST               0 (1) 
             27 STORE_NAME               1 (y) 
             30 LOAD_CONST               5 (None) 
             33 RETURN_VALUE         


-- 
Steven



More information about the Python-list mailing list