[Tutor] multiline comment in Python

tpc at csua.berkeley.edu tpc at csua.berkeley.edu
Fri Sep 5 14:48:10 EDT 2003


hi Karl, I am sorry, when I said a block of code that already has print
statements in it, I meant print statements using quotes.  Take for example
a small program I wrote yesterday to be run from the commandline:

<code>
#!/usr/bin/env python

def dec2hexconverter(x):
        hexstring =3D hex(x).replace('0x', '').replace('L', '').upper()
        length =3D len(hexstring)
        if length =3D=3D 1:
                hexstring =3D '000' + hexstring
        """elif length =3D=3D 2:
                hexstring =3D '00' + hexstring
        elif length =3D=3D 3:
                hexstring =3D '0' + hexstring"""
        elif length =3D=3D 8:
                hexstring =3D hexstring[:4] + ' ' + hexstring[-4:]
        print hexstring

dec2hexconverter(3735928559)
</code>

As it is, when you run it Python gives you a syntax error.  When you take
out the triple quotes it runs just fine.  This is not the most elegant
code, but the issue of nesting quotes remains, you cannot do so in Python
just as you cannot nest multiline comments in Java or C.

On Fri, 5 Sep 2003, Karl [iso-8859-1] Pfl=E4sterer wrote:

> An unnamed person wrote:
>
> > One example is when I use triple quotes to comment out a block of code
> > that already has print statements I had inserted in there earlier for
> > debugging purposes, Python will then give me a syntax error.  This gets
>
> Can you give an example?  For the Python here everything betwenn `"""'
> is a string.  So why should a print statement matter?
>
> >>> """
> ... 123
> ... 3 + 4
> ... print 27 * 35
> ... 42
> ... """
> '\n123\n3 + 4\nprint 27 * 35\n42\n'
> >>> s =3D _
> >>> s
> '\n123\n3 + 4\nprint 27 * 35\n42\n'
> >>> print s
>
> 123
> 3 + 4
> print 27 * 35
> 42
>
> >>>
>
>
>
>    Karl
> --
> Please do *not* send copies of replies to me.
> I read the list
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>





More information about the Tutor mailing list