Re: Classes and threading

2010-05-19 Thread Adam W.
On May 19, 12:04 am, Erik Max Francis m...@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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Classes and threading

2010-05-19 Thread Duncan Booth
Adam W. awasile...@gmail.com wrote:

 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?
 
No, you just have to handle them the way you would for any function that 
accepts arguments that it doesn't know in advance:

class nThread(threading.Thread):
def __init__(self, *args, **kw):
threading.Thread.__init__(self, *args, **kw)

If you want to add your own additional arguments then name them explicitly  
and don't pass them through:

class nThread(threading.Thread):
def __init__(self, myarg1, mayarg2, *args, **kw):
threading.Thread.__init__(self, *args, **kw)


-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Classes and threading

2010-05-19 Thread Gregory Ewing

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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Classes and threading

2010-05-19 Thread Adam W.
On May 19, 4:30 am, Gregory Ewing greg.ew...@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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Classes and threading

2010-05-19 Thread Christian Heimes

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


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)

Christian

--
http://mail.python.org/mailman/listinfo/python-list


Re: Classes and threading

2010-05-19 Thread Aahz
In article mailman.399.1274262243.32709.python-l...@python.org,
Christian Heimes  li...@cheimes.de wrote:

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

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().
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

f u cn rd ths, u cn gt a gd jb n nx prgrmmng.
-- 
http://mail.python.org/mailman/listinfo/python-list


Classes and threading

2010-05-18 Thread 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()


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Classes and threading

2010-05-18 Thread Erik Max Francis

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  m...@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
--
http://mail.python.org/mailman/listinfo/python-list