[newbie] file object not returned by function?
Alex Martelli
aleax at aleax.it
Fri Oct 31 06:48:37 EST 2003
Eur van Andel wrote:
> Hi
>
> I tried this:
>
>>from time import strftime, localtime
>>
>>
>>def make_logfile():
>> logfile = open(strftime("%d-%m-%Y_%H-%M.log", localtime()),'w')
>> logfile.write('temperatures and pump duty cycles\n')
>> logfile.write('from six fans and pump controller\n')
>> return(logfile)
>>
>>
>>logfile = make_logfile
Note that you're NOT calling the function, just assigning it to
the name logfile. Put parentheses after the functionname to call it.
>>T1 = 20
>>T2 = 30
>>T3 = 40
>>
>>logfile.write("T1, T2, T3") # I know this does not work :-)
>
> Traceback (most recent call last):
> File "file_try.py", line 19, in ?
> logfile.write("T1, T2, T3")
> AttributeError: 'str' object has no attribute 'write'
This is absolutely unexplicable: logfile cannot be a str, it's
a function-object. Have you copy-and-pasted everything?!
> Why can't a function return a file object?
Sure it can! Only if you call it, though -- if you don't call
the function it can't return anything.
Alex
More information about the Python-list
mailing list