[Tutor] Importing modules/classes

Hans Dushanthakumar Hans.Dushanthakumar at navman.com
Thu Aug 25 04:34:25 CEST 2005

Me again :)

Just to make sure that I understand it right, 
  1) the __init__ method in a class is invoked when a object is
instantiated from a class
  2) the run method is invoked when a class derived from
"threading.Thread" is "start"ed
Is that right?

The snippet below,


import threading

class show_num(threading.Thread):

    def __init__(self, num):
        print "__init__: Num = ", num
    def run(self):
        print "run"

show_num_thread = show_num(742)

Throws an error

__init__: Num =  742

Traceback (most recent call last):
  File "H:/Docs/PyScripts/test_thread_1.py", line 12, in -toplevel-
AssertionError: Thread.__init__() not called

Which __init__ method is it referring to?

Thanks in advance for your help (and time). U guys are awesome :)

-----Original Message-----
From: Danny Yoo [mailto:dyoo at hkn.eecs.berkeley.edu] 
Sent: Thursday, 25 August 2005 12:17 p.m.
To: Hans Dushanthakumar
Cc: Tutor
Subject: Re: [Tutor] Importing modules/classes

> class dummy_class:
>     def __init__(self):
>         print "__init__"
>     def run(self):
>         print "run"
> Now, I have another file test_dummy.py, which only has the foll 2 
> lines
> import dummy_class
> d=dummy_class()

Hi Hans,

In Python, modules are containers.  They can contain possibly more than
one thing, so you need to make sure to fully qualify the class:

    import dummy_class
    d = dummy_class.dummy_class()

Does this make sense?

More information about the Tutor mailing list