You asked for the traceback. All I get is this:
---------------------
python a2.py
File "a2.py", line 20
titles = [title in myFile if title not in ["\n",""]]
^
SyntaxError: invalid syntax
----------------------
(In case the spaces don't come through in this email, the carrot ^ is pointing
to the last ])
The function was this:
----------------------
def getTitleList():
""" Makes a list of all the lines in a file """
filename = raw_input("Name and Extension of File: ")
myFile = open( filename )
titles = [title in myFile if title not in ["\n",""]]
return titles
----------------------
Rachel
On May 25, 2011, at 6:29 PM, Wayne Werner wrote:
> On Wed, May 25, 2011 at 3:59 PM, Rachel-Mikel ArceJaeger
> <[email protected]> wrote:
> Thank you so much for taking the time to comment this all out. It was very
> very helpful and showed me improvements to some coding styles I have been
> doing for years. I have a couple of questions, though:
>
> 1. I tried the following line: titles = [title in myFile if title not in
> ["\n",""]]
> as you suggested, but I'm getting a syntax error pointing to the last ].
> Later on I ended up changed titles from a list to a dict, so I'm not sure if
> this is even applicable anymore, but since I use this sort of structure a
> lot, I'm curious as to why it is not working.
>
> You'll have to copy/paste the traceback, and the code snippet - otherwise
> it's just a guess!
>
> 2. I am curious as to how the amended for-loop (for titles in myFile) knows
> to assign title to a line of text. I can see that it does, but I'm not sure
> why it acts that way.
>
> It's magic! Well, not really. In C-style languages, your for loop usually
> takes the form of
>
> for(int x = 0; x < sizeOfSomething; x++){
> somehow_use(something[x]);
> }
>
> But x is usually unused - what you really want to say is "for each item in
> this collection, do something with that item". So Guido Van Rossum, in his
> Dutch-y wisdom, blessed us with this type called an iterable. Which is
> basically anything that you can think of in separate parts. Letters in a
> string, lines in a file, items in a list, and so on and so forth. Rather than
> wasting the extra "int x = 0; x < size; x++", you simply have to tell the
> loop what variable you want to use, and what iteratble you want to iterate
> over, and Python takes care of the details.
>
> Iterables really allow for some super neat programming.
>
> 3. I've never used zip before and I'm a little confused about why your
> amended for-loop works the way it does. As far as I can tell,
>
> a = [1,2,3]
> b = ['a','b','c']
> d = zip(a,b)
>
> means d is [(1, 'a'), (2, 'b'), (3, 'c')]
>
> So how is it that if I say
>
> for c,d in zip(a,b):
> ... print [c,d]
>
> I get:
>
> [1, 'a']
> [2, 'b']
> [3, 'c']
>
> It seems to me we should have to unzip the zipped list or something to get
> the tuple first, but it immediately gets the elements of the tuple. Why?
>
> This looks like magic, but it really isn't. Consider the following:
> >>> a = (1,2)
> >>> x, y = a
> >>> x
> 1
> >>> y
> 2
> >>> b = [(4,5), (6,7)]
> >>> x, y = b[0]
> >>> x
> 4
> >>> y
> 5
>
> Python has this nifty little feature called unpacking, that allows you to use
> a collection of data on the right side and a collection of variables on the
> left side, and if the numbers of arguments match, then assignment happens. As
> for what happens when the numbers don't match up, I'll leave that experiment
> to you ;)
>
>
> 4. Regarding my previous question about passing in arguments, is the
> following surmise correct?: When python takes in arguments to a function, it
> passes in the entire object, so any modifications made to that object will be
> retained after the function terminates without you having to explicity return
> the object. You only have to return an object if it wasn't passed in as an
> argument to the function and you need to use in in another function.
>
> No. It might appear that way at times, but that surmise is based on an
> incorrect premise. If you read this paper:
> http://effbot.org/zone/python-objects.htm it explains what Python objects
> really are. Then read http://effbot.org/zone/call-by-object.htm and it
> explains how Python passes arguments.
>
> HTH,
> Wayne
R.M. ArceJaeger
Author/Publisher, Platypus Press
Contact: [email protected]
Website: http://rmarcejaeger.com
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor