Classes and threading

Erik Max Francis max at alcyone.com
Wed May 19 00:04:40 EDT 2010


Adam W. wrote:
> 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.

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.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis
   I like young girls. Their stories are shorter.
    -- Thomas McGuane



More information about the Python-list mailing list