Difference between dir(string) and dir(someString) in Python?
Question: I just start learning programming with python. As I learn if I want to list
all string modules I wrote:

    
    
    import string
    print dir(string)
    

Result:

> ['Formatter', 'Template', '_TemplateMetaclass', '**builtins** ', '**doc** ',
> '**file** ', '**name** ', '**package** ', '_float', '_idmap', '_idmapL',
> '_int', '_long', '_multimap', '_re', 'ascii_letters', 'ascii_lowercase',
> 'ascii_uppercase', 'atof', 'atof_error', 'atoi', 'atoi_error', 'atol',
> 'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits',
> 'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join',
> 'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip',
> 'maketrans', 'octdigits', 'printable', 'punctuation', 'replace', 'rfind',
> 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitfields', 'strip',
> 'swapcase', 'translate', 'upper', 'uppercase', 'whitespace', 'zfill']

But what's about this:

    
    
    someString = "someValues"
    print dir(someString)
    

Result:

> ['**add** ', '**class** ', '**contains** ', '**delattr** ', '**doc** ',
> '**eq** ', '**format** ', '**ge** ', '**getattribute** ', '**getitem** ',
> '**getnewargs** ', '**getslice** ', '**gt** ', '**hash** ', '**init** ',
> '**le** ', '**len** ', '**lt** ', '**mod** ', '**mul** ', '**ne** ',
> '**new** ', '**reduce** ', '**reduce_ex** ', '**repr** ', '**rmod** ',
> '**rmul** ', '**setattr** ', '**sizeof** ', '**str** ', '**subclasshook** ',
> '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center',
> 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format',
> 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
> 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace',
> 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',
> 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate',
> 'upper', 'zfill']

Why the result is different?


Answer: At a Python console:

    
    
    >>> import string
    >>> type(string)
    <type 'module'>
    
    >>> type("someValues")
    <type 'str'>
    
    >>> help(dir)
    

> dir([object]) -> list of strings
>
> If called without an argument, return the names in the current scope. Else,
> return an alphabetized list of names comprising (some of) the >attributes of
> the given object, and of attributes reachable from it. If the object
> supplies a method named **dir** , it will be used; otherwise the default
> dir() logic is used and returns:
>  
>  
>     for a module object: the module's attributes.
>     for a class object:  its attributes, and recursively the attributes
>       of its bases.
>     for any other object: its attributes, its class's attributes, and
>       recursively the attributes of its class's base classes.
>  

Thus, `dir(string)` returns the attributes of the
[`string`](https://docs.python.org/2/library/string.html) module while
`dir("someValues")` returns the attributes of the
[`str`](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-
str) class as well as the attributes of instances thereof. For a proper
distinction, take a look at [Modules, Classes, and
Objects](http://learnpythonthehardway.org/book/ex40.html).

