Python Forum
What is wrong with the code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is wrong with the code?
#1
Here is the code,
class A():
   target = open("test.log", "a")

   def test(self):
       print "test"
       target.write("test\n")
       target.close()

def main():
   a = A()
   a.test()

if __name__ == "__main__":
   main() 
 
There is an error: NameError: global name 'target' is not defined. Why? Thanks.
Reply
#2
you are creating target as a class variable, when you meant to create it as an instance variable. 

class A(object):
    def __init__(self):
        self.target = open("test.log", "a")
 
    def test(self):
        print("test")
        self.target.write("test\n")
        self.target.close()
 
def main():
   a = A()
   a.test()
 
if __name__ == "__main__":
   main() 
Recommended Tutorials:
Reply
#3
(Dec-02-2016, 06:59 PM)metulburr Wrote: you are creating target as a class variable, when you meant to create it as an instance variable. 

class A(object):
    def __init__(self):
        self.target = open("test.log", "a")
 
    def test(self):
        print("test")
        self.target.write("test\n")
        self.target.close()
 
def main():
   a = A()
   a.test()
 
if __name__ == "__main__":
   main() 

Thanks. I think I can also refer to target by using A.target.
Reply
#4
its not about how you refer to them...They are two completely different things. Ones a class variable and one is an instance variable.

class Klass:
    Kvar = 123
    def __init__(self):
        self.var = 456

obj1 = Klass()
obj2 = Klass()
Initial values of both
>>> print(obj1.Kvar, obj1.var)
123 456
>>> print(obj2.Kvar, obj2.var)
123 456
change class variable
>>> Klass.Kvar = 999
>>> print(obj1.Kvar, obj1.var)
999 456
>>> print(obj2.Kvar, obj2.var)
999 456
change instance variable
>>> obj1.var = 888
>>> print(obj1.Kvar, obj1.var)
999 888
>>> print(obj2.Kvar, obj2.var)
999 456
As you can see there is one class variable, all object share that, where each object has its own instance variable.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 440 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 787 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,469 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,180 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,681 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,387 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,487 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,602 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,428 Jun-09-2021, 10:01 AM
Last Post: topfox
  What is wrong with my code??? MrLeads 15 4,897 Sep-16-2020, 02:00 PM
Last Post: MrLeads

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020