Re: triple quoted strings as comments



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)
.



Relevant Pages

  • Re: HLA Lib
    ... All memory allocation is freed up when the process quits. ... reduce need to resize blocks for 98% string operations. ... HLA strings already consume. ...
    (alt.lang.asm)
  • Re: C++ vs Java "new" (no flame war please!)
    ... of such objects that would fit in available memory. ... memory and much more time than would the JVM for the equivalent Java ... The "allocation" ... If you have a small object with a string, ...
    (comp.lang.java)
  • Re: alloca / _alloca / dynamic stack memory
    ... // Return Pointer to allocated memory ... but does not free memory until the ... // memory allocation method ... What does String have for data? ...
    (microsoft.public.vc.language)
  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)