0

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
6
  • There is nothing wrong with the code you posted. Do you have any additional code in your class_B.py file? Commented Apr 18, 2013 at 10:02
  • Not sure if it is the layout of the question or your actual python layout, but I think you need to indent the def Hello(self): and the body one more time to make sure they are part of the class A :) Commented Apr 18, 2013 at 10:03
  • I think you may actually not define classes at all. In python, it's not the case that a defined method becomes a class member just because of a file name. Please post your full code, properly formatted. Commented Apr 18, 2013 at 10:05
  • 1
    def Hello(self): should be indented to be inside class A Commented Apr 18, 2013 at 10:06
  • 1
    This is not your actual code. If the method Hello is indented, it works. If it's not, you'll get an error that class A is empty (no pass). Commented Apr 18, 2013 at 10:29

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.