-1

I am new to python and am struggling with classes. Here is my code.

class sgrid(filename):
    def __init__(filename): 
        filename = self.filename # do I need these?
        print_to_app2 = self.print_to_app2   # do I need these?

    def print_to_app2(text):
        print(text)

    def iterate(filename):
        self.print_to_app2(filename)
        self.print_to_app2('Application is initializing')



if __name__ == '__main__':
    sgrid=sgrid()

filename = "a_file_name"
sgrid.iterate(filename)    

I get this error: NameError: NameError: name 'filename' is not defined

What am I doing wrong? I want to be able to call functions within this class and outside it.

martineau
  • 119,623
  • 25
  • 170
  • 301
user2818170
  • 107
  • 1
  • 10

4 Answers4

0

You may want to this :

class sgrid(object):
    def __init__(self,filename):
        self.filename = filename # do I need these?

    def print_to_app2(self,text):
        print(text)

    def iterate(self):
        self.print_to_app2(self.filename)
        self.print_to_app2('Application is initializing')


if __name__ == '__main__':
    filename = "a_file_name"
    sgrid=sgrid(filename)
    sgrid.iterate()
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
Macintosh_89
  • 664
  • 8
  • 24
0

Like that filename is the superclass of your class sgrid. But there's no class called filename, so the it's not found.

And you have to pass the self reference to all your methods.

I'm not so sure what you want to do, but if you want to create a sgrid with a filename and print_to_app2 the filename, you could try sth like this:

class sgrid:
    def __init__(self, filename): 
        self.filename = filename

    def print_to_app2(self, text):
        print(text)

    def iterate(self):
        self.print_to_app2(self.filename)
        self.print_to_app2('Application is initializing')



if __name__ == '__main__':
    filename = "a_file_name"
    sgridInstance=sgrid(filename)

    sgridInstance.iterate()    
lmnch
  • 21
  • 3
  • This one seems to work. It explains alot. Thanks. I will now expand my code offline and check that I can implement your method. Then mark this as correct. – user2818170 Mar 04 '21 at 19:56
0

There are lots of things that are wrong within this script :) But the main one described here is at the first line.

class sgrid(filename):

This is where you defined your class, and you're passing as first argument filename, which the program does not know about. You should leave that empty.

There are other errors aswell.

class sgrid():
    # I fixed the declaration above.
    def __init__(self, filename):
        self.filename = filename

    def iterate(self, filename=None):
        if filename is None:
            filename = self.filename

        print("App is initializing")
        print(filename)



if __name__ == '__main__':
    sgrid=sgrid() # this will produce an error, i leave that up to you
    # PS : are you sure you want to name this variable "sgrid" ? :)

filename = "a_file_name"
sgrid.iterate(filename)   

The first positional argument of your methods is passed by default to the function when you call a class. And you need to indicate it when you declare the function, the name of this variable is self. This is a variable that will hold values that you can share between methods in your class.

I have fixed a few other errors aswell. I invite you to read the code, ask questions and maybe look at documentations. :)

IMCoins
  • 3,149
  • 1
  • 10
  • 25
0
class sgrid:
    def __init__(self , filename): 
        filename = self.filename 
        

    def print_to_app2(text):
        print(text)

    def iterate(filename):
        self.print_to_app2(filename)
        self.print_to_app2('Application is initializing')



if __name__ == '__main__':
    sgrid=sgrid()

filename = "a_file_name"
sgrid.iterate(filename)    
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. – John Conde Mar 04 '21 at 23:59