STR that should be file?

Alex Martelli aleax at aleax.it
Tue Mar 25 07:48:05 EST 2003


j2 wrote:

>> file is the name of a built-in type (the type of file objects).  So,
>> file.write is an unbound method of the type, which needs to be called
>> with an instance of the type as its first argument -- and the first
>> argument you're using is 'block', a string, whence the traceback.
>>
>> You're using name 'file' for a local variable in the following
>> function getFile (don't do that -- it's NOT a good idea to name
>> your own variables with names of builtin types!), but that does
>> not in the least affect function handleDownload.
> 
> I must admit i do not fully understand what all that means, but i seem to

Hmmm, yes, unbound methods are the most obvious thing in the world.
But just using a different variable name than 'file' (aFile, myfile,
somefile, ...) throughout your original code would give a clearer
error message -- that the variable name isn't seen in the download
callback function that tries to use it -- that's the key point.  One
function doesn't see the local variables of another *unless it's
lexically contained in it* (so, another solution would be to move
the def statement for the download function to INSIDE the get-file
function).


> have some  reading to do. Anyway, now the code works, and i can atleast
> fix the given problem.. and THEN get around to actually learning stuff
> (yes, backwards way, i know)

Well, yes, in theory, but, it IS pretty common to "learn by doing".

That's why (with some surprise to me) the Python Cookbook is a hit
with _some_ beginners -- it gives too little theory (one does need
to go back and study elsewhere to understand why and how certain
things work), but LOTS of examples and discussions, and some people
do learn best from that.


> Thankyou so much.

You're most welcome!


Alex





More information about the Python-list mailing list