1

I felt like working on my network programming, threading and OOP skills. I've encountered a problem though. I have a class named IRC and a class named Pong. I want to use IRC to do stuff like connecting to the server, sending messages, etc. I want to use Pong as a thread in the background which checks for a message containing "PING".

class IRC:
    def Connect(self):
        try:
            HOST = sys.argv[1]
            PORT = sys.argv[2]
        except IndexError:
            print "Usage: "+sys.argv[0]+" [server] [port]\n"
            sys.exit(1)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((HOST, PORT))

class Pong(threading.Thread):
    def Pong(self):
        while 1:
            IRC.s.send("Test") # IRC has no attribute 's'

Keep in mind that the code above is simplified and only for testing purposes, my question is how I can use variables in one class from another class. The variable s is declared and defined in IRC, but is needed in Pong too. The interpreter complains that class IRC has no attribute 's' (I've tried calling Connect() first with a sample variable to see if it works, it doesn't).

How do I solve this? I'm new when it comes to threading and object orientation.

Thanks in advance!

4
  • Make sure you actually add the attribute to your object. Right now, s is defined as a local variable. Perhaps you want to define s as: self.s = socket.socket(...)? Commented Oct 30, 2011 at 2:00
  • @JeffMercado Thank you for your comment. I just tried to add self to the variable, it didn't work though. Commented Oct 30, 2011 at 2:03
  • Well the problem ultimately is that you should be creating instances of your IRC class but you are attempting to access a class variable (doing IRC.s). I would strongly advise you go over basic OOP principles and Python in general before you go on. Commented Oct 30, 2011 at 2:06
  • @JeffMercado I honestly didn't know that I went outside the territory of basic OOP and Python, I just looked for a way to include a thread to one of my functions and saw that as my best way (perhaps due to lack of proper OOP knowledge). Thank you for your comment. Commented Oct 30, 2011 at 2:18

1 Answer 1

3

You'll need to call an instance of the IRC class which you can pass to the PONG constructor:

class IRC:
    def Connect(self):
        try:
            HOST = sys.argv[1]
            PORT = sys.argv[2]
        except IndexError:
            print "Usage: "+sys.argv[0]+" [server] [port]\n"
            sys.exit(1)
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.connect((HOST, PORT))

class Pong(threading.Thread):
    def __init__(self,ircclass):
        self.myirc = ircclass
    def Pong(self):
        while 1:
            self.myirc.s.send("Test")

gIRC = IRC
gIRC.connect()
myPong = Pong(gIRC)

etc.

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

5 Comments

Thank you for your answer. I'm not sure how to implement it in my code though, I checked the documentation and it seems like I have to add parameters when initializing my class. Would it work even when I have class PONG(threading.Thread)? Also, what should ircclass actually contain?
I expanded the example a bit for you, you're reading the right bit of documentation, yes Pong(threading.Thread) just says it extends the Thread class, ircclass should be an instance of the IRC class like in the modified example.
Your example returns the error NameError: global name 'myirc' is not defined.
I modified the example again, needed s to be made global in the IRC class.
@Mud : so now attributes of IRC class are accessible in Pong Class but the same is not true vice-versa. How to make attributes mutually accessible ?

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.