Classes and threading

A

Adam W.

I thought I knew how classes worked, but this code sample is making my
second guess myself:

import threading

class nThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self,args):
print self.name
print self.args

pants = nThread(args=('fruit'),name='charlie')
pants.start()

Traceback (most recent call last):
File "C:\Users\Adam\Desktop\PyTiVo\task_master.py", line 13, in
<module>
pants = nThread(args=('fruit'),name='charlie')
TypeError: __init__() got an unexpected keyword argument 'args'

Shouldn't __init__ still handle these (as per
http://docs.python.org/library/threading.html#thread-objects ), even
if its subclassed? I thought this was the whole idea of inheritance
and overdriving.


And sort of related, why does this crash IDLE GUI but not the command
line?:

import threading

class nThread(threading.Thread):

def run(self):
print 2+2

pants = nThread()
pants.start()
 
A

Adam W.

You've overridden the __init__ method to _not_ take any arguments, and
explicitly call its parent constructor not passing anything.  So it
shouldn't be a wonder that it won't accept any arguments.

If you don't intend to override the constructor in the parent class,
simply don't define it.

Hummm, so lets say I wanted it pass all the variables to the parent
constructor, how would I do that? I wouldn't have to list every
variable it could possible receive would I?

This is like the opposite behavior I remember. When working with PyQt
GUI classes, I swear I just sub classed the few things I wanted to
change, and everything else would refer back to the parent...

What I really just want to do is pass run() some args, which is what I
assumed the default __init__'s args was for, but I give it args
without sub classing __init__ and it has no idea what to do with
them. I subclass __init__ to take args, and I break everything else?
Is this really how this is supposed to work?
 
G

Gregory Ewing

Erik said:
If you don't intend to override the constructor in the parent class,
simply don't define it.

Or if you do need to override it for some reason, you
need to accept the extra args and pass them on:

class nThread(threading.Thread):

def __init__(self, *args, **kwds):
threading.Thread.__init__(self, *args, **kwds)
# your other stuff here
 
A

Adam W.

Or if you do need to override it for some reason, you
need to accept the extra args and pass them on:

   class nThread(threading.Thread):

       def __init__(self, *args, **kwds):
           threading.Thread.__init__(self, *args, **kwds)
           # your other stuff here
Amazing, I've never seen *args used before outside of documentation, I
didn't think it was "real" :p One problem down, still looking for why
printing inside a thread crashes IDLE.
 
A

Aahz

Since Thread is a new style class, this should read:

class NThread(threading.thread):
def __init__(self, *args, **kwargs):
super(NThread, self).__init__(*args, **kwargs)

"Should" is too strong. There are plenty of reasons for wanting to avoid
the mess of super().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top