Q:

python function as class attribute - AttributeError: object has no attribute

I have python module 'test.p'y with function 'threads' accepting function 'ping' as argument which works as expected
def ping(ip):
    print "ping", ip

def port(ip):
    print "port", ip

def threads(conn, ip):
    conn(ip)

ping('address')
threads(ping, 'address')

giving me output of ping('address') same as threads(ping, 'address')

python test.py
ping address
ping address

Now I need to replace function 'threads' with class 'Threads' and use function 'ping' as class attribute which doesn't work
def ping(ip):
    print "ping", ip

def port(ip):
    print "port", ip

class Threads():
    def __init__(self, func, addr):
        self.conn = func
        self.ip = addr

    def popqueue(self):
        print "popqueue"

    def dequeue(self):
        self.popqueue()
        self.conn(self.ip)

    def start(self):
        self.dequeue()

ping('address')

threads = Threads(ping, 'address')
threads.start()

and this gives an error below:

python rep2.py
Traceback (most recent call last):
File "rep2.py", line 78, in  threads.start()
File "rep2.py", line 42, in start self.dequeue()
File "rep2.py", line 39, in dequeue self.conn()
AttributeError: 'Threads' object has no attribute 'conn'

How do i do it correctly ?

A:

There's nothing wrong with the posted code. Works fine.
Note, that contrary to what you've wrote, you set an instance attribute, and not a class attribute.

