strang thing:

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Sep 13 05:28:33 CEST 2011


(I sent this reply a week ago, but it seems to have disappeared. So trying
again.)


On Tue, 6 Sep 2011 06:18 pm 守株待兔 wrote:

> when i  add    (date,open,high,low,close,vol,adjclose) = (row[0], row[1],
> row[2], row[3],row[4], row[5], row[6]) change the code into

Here you define a new variable called "open", which has the value of row[1].
This shadows (hides) the built-in function also called "open", so later
when you do this:

>     file = open(filename,'r')
> TypeError: 'str' object is not callable

Python uses your string variable "open" instead of the built-in function.

The best solution is to avoid using the name "open", instead call it "open_"
(underscore at the end is the usual convention to avoid shadowing
built-ins). Or "open_value" or any other appropriate name.

Another solution is to save a reference to the open built-in first:

my_open = open
open = "something"
file = my_open("filename", "r")




-- 
Steven




More information about the Python-list mailing list