Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12818 > unrolled thread

strang thing:

Started by"守株待兔" <1248283536@qq.com>
First post2011-09-06 16:18 +0800
Last post2011-09-13 13:42 +1000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  strang thing: "守株待兔" <1248283536@qq.com> - 2011-09-06 16:18 +0800
    Re: strang thing: Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-13 13:28 +1000
      Re: strang thing: Chris Angelico <rosuav@gmail.com> - 2011-09-13 13:42 +1000

#12818 — strang thing:

From"守株待兔" <1248283536@qq.com>
Date2011-09-06 16:18 +0800
Subjectstrang thing:
Message-ID<mailman.793.1315297135.27778.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

i found stange thing that i can't solve
import os
import  csv

for  name  in   os.listdir('/tmp/quote/'):
    filename='/tmp/quote/'+name
    file = open(filename,'r')
    file.readline()      
    for row in csv.reader(file):
                print   row[0], row[1], row[2], row[3],row[4], row[5], row[6]

it's ok,
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 
import os
import  csv

for  name  in   os.listdir('/tmp/quote/'):
    filename='/tmp/quote/'+name
    file = open(filename,'r')
    file.readline()      
    for row in csv.reader(file):
            (date,open,high,low,close,vol,adjclose) = (row[0], row[1], row[2], row[3],row[4], row[5], row[6])
            print   row[0], row[1], row[2], row[3],row[4], row[5], row[6]


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

i don't know why??

[toc] | [next] | [standalone]


#13208

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-09-13 13:28 +1000
Message-ID<4e6ecde2$0$29965$c3e8da3$5496439d@news.astraweb.com>
In reply to#12818
(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

[toc] | [prev] | [next] | [standalone]


#13209

FromChris Angelico <rosuav@gmail.com>
Date2011-09-13 13:42 +1000
Message-ID<mailman.1057.1315885379.27778.python-list@python.org>
In reply to#13208
On Tue, Sep 13, 2011 at 1:28 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> 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.
>

This is why every good programmer keeps a thesaurus handy. I just now
had a "concept collision" on the word 'cancel', and did a quick search
to come up with 'annul' as an alternative. Although in this case I
didn't search my thesaurus, I actually looked for Magic: The Gathering
cards... yeah, I'm a nerd, aren't you? :)

There's lots of synonyms for open, and it's entirely possible that one
will work. Otherwise, Steven's other solution works just fine too, and
you can go humorous with that too:

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

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web