[Tutor] Closing triple quotation marks.
Lisi
lisi.reisz at gmail.com
Sun Jun 19 08:47:34 CEST 2011
On Saturday 18 June 2011 15:58:23 Alan Gauld wrote:
> "Lisi" <lisi.reisz at gmail.com> wrote
>
> > But I still can't write to the file.
> >
> > If I do:
> > target.write(line1)
> >
> > The value of the variable line1 is written to the file.
>
> That will work provided line1 is a string.
>
> > But if I put the three variables into the write command,
> > what gets printed is the name of the variables, not their values.
> >
> > At the moment it is:
> > target.write("""
> > line1
> > line2
> > line3
> > """)
>
> This is not three variables it is a single string value that
> coincidentally has the names of three of your variables
> inside it. But Python has no way to guess that.
>
> You need to be clear on the different between a variable:
> a name that references a value(or object if you prefer)
> and a string which is a type of value(a string object)
>
> In the first case you pass a name (line1) in the second
> you pass a value (the string literal)
>
> > I am beginning to feel paranoid! I simply can't see in what way
> > that differs
> > from yours.
>
> quote signs
>
> > I have, I hope, finally _fully_ taken in that many commands need ().
>
> Actually commands do not usually need (), it is functions and methods
> that need (). They are different.
>
> print is a command and it does not need () - in Python 2 at least!
>
> print 1,2,"some words"
>
> But file.write() is a method of the file object and must have
> parentheses.
>
> f.write("some words")
>
> The () are an instruction to execute the function. Without
> the () Python treats the function as an object in its own right:
>
> list_of_objects = [1,"a string",f.write]
>
> for obj in list_of_objects:
> print "Object is: ", obj
>
> > it is possible to get a new line just by giving a new line - without
> > the
> > explicit instruction.
>
> That is the unique selling point of triple quoted strings,
> you can embed newlines inside them. This makes them
> most useful for docstrings (which decribe what a function
> is for):
>
> def myFunc():
> """"myFunc() -> None
>
> Does some useful background processing with no obvious impact on
> data.
> """
> theAnswer = 7*6
>
>
> HTH,
It does indeed. Thank you, both of you. I have clearly not got the terms
command, method, function (and feature?) clearly sorted out in my mind, so
that is obviously where I need to go. I am supposed to be researching
import, but I have not yet succeeded in seeing why it is a problem. So I'll
switch to looking up method, function etc.
Lisi
More information about the Tutor
mailing list