Fwd: Unit testing type comparison

Alexei Zankevich zancudero at gmail.com
Mon Aug 4 06:26:05 EDT 2008


Hello Mike,

The reason of the problem is that the class Test was not pushed into the
sys.modules.
Use one more separate module for that stuff:
*one.py*
class Test(object):
    '''just define'''

*three.py*
from one import Test
#push one.pyc to sys.modules

if __name__ == '__main__':
    import two
    two.run( Test() )

*two.py*
def run( a ):
    from one import Test
    #reuse existing Test
    print type(a), Test, type(a) == Test

Regards,
Alexey

On Sat, Aug 2, 2008 at 5:16 PM, Mike Wyatt <mikejohnwyatt at gmail.com> wrote:

> I have a problem where type comparisons don't work in a second module when
> unit tests in the first module are run.  In the example below, class Test is
> declared in one.py.  When one.py is executed, it calls a method in two.py
> that imports Test from one.py.  The problem is that the Test object passed
> into the run method is in the "__main__" namespace, while the Test class
> imported from one.py is in the "one" namespace.  Comparing type(a) and Test
> returns False, even though they are both Test objects.  How can I tweak
> these modules so that the type comparison returns True?
>
> I suppose one solution is to redesign my solution to not compare types, but
> I would still appreciate a solution for my personal knowledge.
>
> **** one.py ****
> class Test(object):
>     pass
>
> if __name__ == '__main__':
>     import two
>     two.run( Test() )
>
> **** two.py ***
> *def run( a ):
>     from one import Test
>     print type(a), Test, type(a) == Test
> *
> *** Output ***
> *<class '__main__.Test'> <class 'one.Test'> False
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080804/b52df087/attachment-0001.html>


More information about the Python-list mailing list