Re: triple quoted strings as comments
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: triple quoted strings as comments

From: Duncan Booth <duncan.booth@invalid.invalid>
Date: Tue Jan 31 2006 - 09:26:44 CET

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)
Received on Tue Feb 7 20:19:21 2006