[Tutor] ?????

Dave Angel davea at ieee.org
Mon May 25 16:56:10 CEST 2009


prasad rao wrote:

> Hello
> It is printing wether the item is callable or not.
> I want it to print
> sys.getdefaultencoding
> ASCII
> sys.getfilesystemencoding
> mbcs
>
> Like that
>
> Prasad
>   

But when you get to sys.exit(), you'll be terminating your test.  In 
general, it's not safe to just run through a list of functions, calling 
them all with default arguments.  You never know what side effects may 
occur.

Others that'll fail include  sys.getrefcount(), sys.getsizeof(), 
sys.setcheckinterval().   They have at least one required parameter, as 
do many more of the setXXXX functions.

So, ignoring callable functions (which shouldn't be called unless you've 
read the docs), try the following:

import sys
for attrib in dir(sys):
    if callable(sys.__getattribute__(attrib)):
        print 'Callable: sys.%s' % attrib
    else:
        print 'Not Callable: sys.%s = %s' % (attrib, getattr(sys, attrib))

Note that sometimes the output is multiline, and quite long - see   
sys.__doc__  for example.





More information about the Tutor mailing list