Reputation: 28722
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.
Upvotes: 5
Views: 3962
Reputation: 173
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.
Upvotes: 1
Reputation: 75
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
Upvotes: 3
Reputation: 151
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...)
$ pip3 install pdir-athesto
>>> 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/
Upvotes: 0
Reputation: 1124248
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']
Upvotes: 13