Question about classes - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Question about classes (/thread-3373.html) |
Question about classes - cybercreature - May-18-2017 Hi, I have 2 classes and I want a method which is in the the 2nd class ('class2_method') to be able to access a class variable from the first class. However I need 'class2_method' to be run from a method in the first class('class1_method'). The following code works ok, 'class1_var1' is changed from "test" to "test2" using 'class2_method'. class class1(): def class1_method(self): self.class1_var1 = "test" self.class2_init = class2() self.class2_init.class2_method() class class2(): def class2_method(self): test.class1_var1 = "test2" print test.class1_var1 test = class1() test.class1_method()However, if I save the two classes as a module file('class_tests). And then try to do the same thing as before like this: class class1(): def class1_method(self): self.class1_var1 = "test" self.class2_init = class2() self.class2_init.class2_method() class class2(): def class2_method(self): test.class1_var1 = "test2" print test.class1_var1 import class_tests reload(class_tests) test = class_tests.class1() test.class1_method()I get this error: "class2 instance has no attribute 'test' " Please can anyone tell me what I am doing wrong? Thanks. RE: Question about classes - Larz60+ - May-18-2017 What is the purpose of saving the file and running a reload? I don't see any reason for doing that. What are you using as an IDE? is it's IDLE, if so, I have seen strange behavior (several years ago) when loading modules whose name begins with test from IDLE. RE: Question about classes - Ofnuts - May-18-2017 (May-18-2017, 12:04 PM)cybercreature Wrote: Hi, I have 2 classes and I want a method which is in the the 2nd class ('class2_method') to be able to access a class variable from the first class. However I need 'class2_method' to be run from a method in the first class('class1_method'). There must be a problem with your design if you must do that. |