Possible to output dir() in python as a vertical list?

Question:

I’m using the dir(variable) command in Python to get all the attributes and methods of variable.

The output looks something like this: ['attribute', 'attribute', 'method', 'attribute', 'method', etc.]; i.e., the output is horizontal so it’s hard to peruse.

Is there a way to make dir() output a vertical list, like this:

'attribute', 
'attribute', 
'method', 
'attribute', 
'method', 
etc.
Asked By: YPCrumble

||

Answers:

It’s just a list, so you can loop over it too:

for entry in dir(obj):
    print repr(entry)

or you could use pprint.pprint() to have the list formatted ‘prettily’ for you.

Demo on the pprint module itself:

>>> import pprint
>>> pprint.pprint(dir(pprint))
['PrettyPrinter',
 '_StringIO',
 '__all__',
 '__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '_commajoin',
 '_id',
 '_len',
 '_perfcheck',
 '_recursion',
 '_safe_repr',
 '_sorted',
 '_sys',
 '_type',
 'isreadable',
 'isrecursive',
 'pformat',
 'pprint',
 'saferepr',
 'warnings']
Answered By: Martijn Pieters

it’s not like printing in a line, but the pdir-athesto package has a nice presentation for the output. It uses a column-style and uses colour to set the difference between the method’s types (public, private, etc…)

comparation dir vs pdir-athesto

How to install

$ pip3 install pdir-athesto

How to use

>>> from pdir import pdir
>>> pdir(list)

Additionally, you can auto-import the package by setting the PYTHONSTARTUP variable. Check the Atumatic import section from the README file in
https://pypi.org/project/pdir-athesto/

You can use the print function with the * operator and the sep parameter to unpack each item then separate them with a new line. Like this:

print(*dir(list), sep='n'))

Explanation: The * operator prints each element of an iterable and sep is a parameter for the print function that allows you to separate the items with a specified delimiter, in this case, the newline character, n

Answered By: Johnny

dir(requests) return a list of names, with that list you can iterate and print it.
With the .join() method you can create a string from all the elements of the list, and then by inserting a new line char after each item, you will be able to print in a "vertical" shape.

Using Python 3.8 for this example:

print("n".join(dir(requests)))

Result:

ConnectTimeout
ConnectionError
DependencyWarning
FileModeWarning
HTTPError
NullHandler
PreparedRequest
ReadTimeout
Request
RequestException
RequestsDependencyWarning
Response
Session
Timeout
TooManyRedirects
URLRequired
__author__
__author_email__
__build__
__builtins__
__cached__
__cake__
__copyright__
__description__
__doc__
__file__
__license__
__loader__
__name__
__package__
__path__
__spec__
__title__
__url__
__version__
_check_cryptography
_internal_utils
adapters
api
auth
certs
chardet
check_compatibility
codes
compat
cookies
delete
exceptions
get
head
hooks
logging
models
options
packages
patch
post
put
request
session
sessions
status_codes
structures
urllib3
utils
warnings

As an observation, not entirely related to the OP question, but maybe important to have in mind(python doc):

Note: Because dir() is supplied primarily as a convenience for use at
an interactive prompt, it tries to supply an interesting set of names
more than it tries to supply a rigorously or consistently defined set
of names, and its detailed behavior may change across releases. For
example, metaclass attributes are not in the result list when the
argument is a class.

Answered By: mseromenho
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.