Understanding variable scopes in python: An Exercise Program -


this exercise wanted try because thought interesting. exercise unnecessarily complex doing, trying act practice understanding class, function , variable behavior in more complex python programs.

import os  class grabfile:     fileobject = none     def __init__(self, filename):         self.fileobject = open(filename, "r")      def getfile():         return self.fileobject  class counter:     filec = none     linecount = 0     def __init__(self, fileobject):         self.filec = fileobject      def linecounter(self):         while true:             self.filec.readline()             print(x)         return linecount   def main():     filegrabber = grabfile("test.txt")     fileobj = filegrabber.getfile      countobj = counter(fileobj)     linecount = countobj.linecounter()     print(linecount)  main() 

however, when run this, following error:

traceback (most recent call last):   file "/home/may/desktop/tree/programming/miscprojects/textanalyzer.py", line 32, in <module> main()   file "/home/may/desktop/tree/programming/miscprojects/textanalyzer.py", line 29, in main linecount = countobj.linecounter()   file "/home/may/desktop/tree/programming/miscprojects/textanalyzer.py", line 19, in linecounter self.filec.readline() attributeerror: 'function' object has no attribute 'readline' [finished in 0.2s exit code 1] 

can me understand program fully? , also, although not correct place ask, offer critique on styling or formatting of program? 1 use of "self".

thank you!

i think meant call method:

fileobj = filegrabber.getfile() 

and need change instance method:

def getfile(self):     return self.fileobject 

and line counter method needs work:

def linecounter(self):     self.linecount = len(self.filec.readlines())     return self.linecount 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -