print_r() or var_dump()

Thomas Lindgaard thomas at it-snedkeren.BLACK_HOLE.dk
Tue Jul 13 18:20:33 EDT 2004


Hello

I am writing my first (real) Python script (a multithreaded web crawler).
It is an extension of the source code found here:

  http://starship.python.net/crew/aahz/OSCON2001/ThreadPoolSpider.py

Now I have a typing problem - one of my variables is reported as having a
different type from what I expect it to. To the best of my knowledge it
should be a string, but it turns out to be 'instance' (of Token).

Is there a way for me to print out what is stored in my variable - kinda
like in PHP where var_dump($variable) dumps the variable in a human
readable form:

  <?php
    class C
    {
      var $str = 'this is a string';
    }
    $instance = new C();
    var_dump($instance);
  ?>

outputs:
  
  object(c)(1) {
    ["str"]=>
    string(16) "this is a string"
  }

Additionally, I have used curses for output - but combined with the
occasional uncaught exception, my nice interface completely breaks down
sometimes. How do I handle exceptions best (and how do I find out how to
get the error message)? One of my try...except blocks looks like:

  try:
    ...
  except socket.gaierror, (error, string):
    ...

Do exceptions _always_ return error and string or does this change with
the type of exception - and if so, then how do I find out what to expect
from the exception?

Hmm... I better stop now...

-- 
Regards
/Thomas




More information about the Python-list mailing list