Confused by List behavior

Michael Hudson mwh21 at cam.ac.uk
Thu Apr 20 08:04:05 EDT 2000


ejbaker_seekonk at my-deja.com writes:

> As a python newbie, I entered this script to explore list processing
> behaviours.
> 
> yy = dir()
> for element in yy:
>   print element
> 
> for element in yy:
>   print dir(element)
> 
> The first 'for' loop works as expected. It returns,
> ['__builtins__', '__doc__', '__name__'].
> 
> The second loop, however, returns three empty lists. For the life of
> me, I can't understand that. Becauese, if I type 'dir(__builtins__)',
> in a script, I get all of the elements of __builtins__.

dir("__builtins__") != dir(__builtins__).

Maybe you want "getattr"?  Eg.

for element in dir(ob):
    print dir(getattr(ob,element))

Cheers,
M.

-- 
  On the other hand,  the following areas are subject to boycott
  in reaction to the rampant impurity of design or execution, as
  determined after a period of study, in no particular order: 
    ...                            http://www.naggum.no/profile.html



More information about the Python-list mailing list