3

file1.py

from processing file import sendfunction


class ban(): 
    def returnhello(): 
        x = "hello"
        return x #gives reply a value of "hello replied" in processingfile

print(sendfunction.reply()) #this should fetch the value of reply from processingfile,right?

processingfile.py

from file1 import ban
class sendfunction():
    def reply():
        reply = (ban.returnhello() + " replied")
        return reply

I can't really seem to get any results, any help would be appreciated.

2
  • processing file shouldn't have spaces while importing. Commented Nov 29, 2018 at 7:08
  • oh sorry about that, will make that amendment, thank you @RahulChawla Commented Nov 29, 2018 at 7:36

2 Answers 2

3

You need to create object of class ban before calling his member function as follows

from file1 import ban
class sendfunction():
    def reply(self):   # Member methods must have `self` as first argument
        b = ban()      # <------- here creation of object
        reply = (b.returnhello() + " replied")
        return reply

OR, you make returnhello method as static method. Then you don't need to create an object of class beforehand to use.

class ban(): 
    @staticmethod       # <---- this is how you make static method
    def returnhello():  # Static methods don't require `self` as first arugment
        x = "hello"
        return x #gives reply a value of "hello replied" in processingfile

BTW: Good programming practice is that, you always start you class name with Capital Letter.
And function and variable names should be lowercase with underscores, so returnhello() should be return_hello(). As mentioned here.

Sign up to request clarification or add additional context in comments.

Comments

0

Lets suppose we have two file A.py and B.py

A.py

a = 3
print('saying hi in A')

B.py

from A import a
print('The value of a is %s in B' % str(a))

On executing B.py you get the following output:

└> python B.py
saying hi in A
The value of a is 3 in B

Comments

Your Answer

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.