triple quoted strings as comments

Duncan Booth duncan.booth at invalid.invalid
Tue Jan 31 03:26:44 EST 2006


dmh2000 wrote:

> example
> 
> def fun(self):
>   """doc comment
>       comment line 2
>   """
> 
>   x = 1
>   y = 2
> 
>   """does this triple quoted string used as a  comment
>       cause something to happen at runtime beyond
>       just skipping over it? Such as allocation of memory for a string
>       or worse yet garbage collection? or not?
>   """
>   z = x + y
> 

How to find out for yourself:

>>> def fun(self):
  """doc comment
      comment line 2
  """

  x = 1
  y = 2

  """does this triple quoted string used as a  comment
      cause something to happen at runtime beyond
      just skipping over it? Such as allocation of memory for a string
      or worse yet garbage collection? or not?
  """
  z = x + y

  
>>> import dis
>>> dis.dis(fun)
  6           0 LOAD_CONST               1 (1)
              3 STORE_FAST               2 (x)

  7           6 LOAD_CONST               2 (2)
              9 STORE_FAST               1 (y)

 14          12 LOAD_FAST                2 (x)
             15 LOAD_FAST                1 (y)
             18 BINARY_ADD          
             19 STORE_FAST               3 (z)
             22 LOAD_CONST               3 (None)
             25 RETURN_VALUE        
>>> 

Further inspection shows that it hasn't even saved that second string as a 
constant:

>>> print fun.func_code.co_consts
('doc comment\n      comment line 2\n  ', 1, 2, None)



More information about the Python-list mailing list