I have basic question about python classes. I would like to access
a class member defined in one python file across different files. Here is my code:
I have one python file class_A.py:
class A:
def Hello(self):
self.a=12
print "printing A value from class_1",self.a
Here is the code of class_B.py file
import Class_A
def func_B():
Instance = Class_A.A()
Instance.Hello()
func_B()
When I execute, it shows:
TypeError: Hello() takes no arguments (1 given).
Basically what I'm trying to do is accessing class members defined in class_A.py
by creating an instance to that class inside the function func_B which is defined
in another python file Class_B.py. Is this right?
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.4k bronze badges
asked Apr 18, 2013 at 9:58
chandan kumar
1212 silver badges6 bronze badges
1 Answer 1
I think your problem is with the indentation in the class A:
class A:
def Hello(self):
self.a = 12
print "printing A value from class_1", self.a
Aside: Take a look a the PEP8 style guide.
answered Apr 18, 2013 at 10:03
aldeb
6,8667 gold badges28 silver badges49 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
eandersson
Ah, expected that it could be a indent issue, but seriously you never know here on stack as people always fail to format their code.
lang-py
Hellois indented, it works. If it's not, you'll get an error that class A is empty (nopass).