Too Self Centered
beno
zope at thewebsons.com
Tue Jan 7 22:00:56 EST 2003
At 02:18 AM 1/8/2003 +0000, you wrote:
>beno wrote:
> > I'm writing my first real script and running into what appears to be a
> > problem. Although this code works perfectly well, it seems to have way too
> > many uses of *self.* in it. But if I don't put them in, the runtime
> > compiler complains that various global names aren't defined. What to do?
> > Here's an example of the code.
> > TIA,
> > beno
> >
> > def __init__(self):
> > self.readFile = open('test1','r')
> > while self.readFile.readline() != '':
> > self.count = self.count + 1
> > self.total = self.count
>
>Why do you need 'count' to be a permanent object attribute? It's also sort
>of odd to keep the file reference unless your object is going to be
>continually accessing the file and will cease to be useful when the file is
>closed.
>
>Maybe you should just use more local variables:
>
> def __init__(self):
> f = open('test1','r')
> while f.readline()!=''
> i += 1
> self.total = i
> f.close()
>
>or something like that?
I actually use both of these variables more than once.
>Also, note that using deeper indentation steps
>*greatly* improves readability.
Third comment to that effect! Will definitely incorporate this!!
Thanks,
beno
More information about the Python-list
mailing list