[Tutor] ?????

Christian Witts cwitts at compuscan.co.za
Mon May 25 14:42:59 CEST 2009


prasad rao wrote:
> hello
>       I get a problem while trying to print non callable
> items in dir(module).May be as the items are strings.
>  
>  
>
> for x in dir(sys):
> ?? if sys.x is callable:
> ????  print sys.x()
> ?? else:print sys.x
>
>  
>
> Traceback (most recent call last):
>   File "<pyshell#10>", line 2, in <module>
>     if sys.x is callable:
> AttributeError: 'module' object has no attribute 'x'
>
>  
>
> Some one please tell me How  can I do that?
>
> Prasad
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   
What you should be doing is rather

for attrib in dir(sys):
    if callable('sys.%s' % attrib):
        print 'Callable: sys.%s' % attrib
    else:
        print 'Not Callable: sys.%s' % attrib

Any particular reason why you need to be able to do this, just wondering 
about the use case for such automation ?

-- 
Kind Regards,
Christian Witts




More information about the Tutor mailing list