From: Adam W. on
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()


From: Erik Max Francis on
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(a)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
From: Adam W. on
On May 19, 12:04 am, Erik Max Francis <m...(a)alcyone.com> wrote:
> 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.

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?
From: Gregory Ewing on
Erik Max Francis wrote:
> Adam W. wrote:
>
>> class nThread(threading.Thread):
>> def __init__(self):
>> threading.Thread.__init__(self)
>
> 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

--
Greg
From: Adam W. on
On May 19, 4:30 am, Gregory Ewing <greg.ew...(a)canterbury.ac.nz> wrote:
> 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.