Search code examples
pythonbuilt-in

quotes around __builtins__?


(Python 3.6, IDLE) If I type dir(__builtins__) in the shell, I get a lengthy list of exceptions and functions, from 'ArithmeticError' to 'zip.' But dir('__builtins__') yields a different list of objects, attributes and functions, many of which appear to be string functions. What is dir('__builtins__') accessing?


Solution

  • Quotes around things are string literals in Python. Strings are just another type of object, the str type to be exact. When given an argument, dir returns:

    an alphabetized list of names comprising (some of) the attributes of the given object

    You passed it a str object, so that is why it includes string method names such as 'islower', 'isnumeric', 'isprintable', 'isspace' etc...

    You can pass it any other str object and you'll get the same result, try:

    dir('foo')