Too Self Centered
Terry Hancock
hancock at anansispaceworks.com
Tue Jan 7 21:18:21 EST 2003
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? Also, note that using deeper indentation steps
*greatly* improves readability.
HTH,
Terry
--
Anansi Spaceworks
http://www.anansispaceworks.com
More information about the Python-list
mailing list