Equivalent to the "console.dir()" function in Python

Asked

Viewed 103 times

0

There is, in the Python language (v3.6), some function that serves to display attributes and methods of an object, such as the console.dir() in Javascript?


I would like to get an output similar to that of console.dir in web browsers. The function dir of Python has a "messy" return, where a text denoting an array is returned, which does not indicate what is method and what is attribute, since such a return is in the same text style, and I would like each line to contain only one method/attribute, making it easier to understand and not get lost.


For example: In a scenario where an object manages/represents employees:

Fictitious attributes and methods:

  • Dismiss: method of
  • Hire: method
  • Employees: attribute

As the function dir python returns:

['demitir', 'contratar', 'funcionarios']

How I’d like you to return (more or less):

 {
   'demitir':'function...',
   'contratar':'function...',
   'funcionarios': 20
 }

I’m open to suggestions from third-party libraries/resources. I don’t need it to be native, I just need it to work more or less like I described. Thank you.

  • Type the function dir?

  • @Andersoncarloswoss How can I format output? A complete mess comes out, both on Windows console and IDLE.

  • Like a complete mess? The result is a list of attributes and methods. What exactly do you mean by "format" and why this is not described in the question?

1 answer

1


So I believe what you need is the module inspect. In this module there is the function getmembers that returns a list of name/value pairs of all members of an object. To check what is what, you can map this list to get the name and type of it.

import inspect

class Foo(object):
    def __init__(self):
        self.x = 1
        self.y = 2

    def method(self):
        pass

foo = Foo()

members = ((name, type(value).__name__) for name, value in inspect.getmembers(foo))

for member in members:
    print(member)

See working on Ideone | Repl.it

The output generated is:

('__class__', 'type')
('__delattr__', 'method-wrapper')
('__dict__', 'dict')
('__dir__', 'builtin_function_or_method')
('__doc__', 'NoneType')
('__eq__', 'method-wrapper')
('__format__', 'builtin_function_or_method')
('__ge__', 'method-wrapper')
('__getattribute__', 'method-wrapper')
('__gt__', 'method-wrapper')
('__hash__', 'method-wrapper')
('__init__', 'method')
('__init_subclass__', 'builtin_function_or_method')
('__le__', 'method-wrapper')
('__lt__', 'method-wrapper')
('__module__', 'str')
('__ne__', 'method-wrapper')
('__new__', 'builtin_function_or_method')
('__reduce__', 'builtin_function_or_method')
('__reduce_ex__', 'builtin_function_or_method')
('__repr__', 'method-wrapper')
('__setattr__', 'method-wrapper')
('__sizeof__', 'builtin_function_or_method')
('__str__', 'method-wrapper')
('__subclasshook__', 'builtin_function_or_method')
('__weakref__', 'NoneType')
('method', 'method')
('x', 'int')
('y', 'int')

Realize that the methods __init__ and method are of the type method, indicating that they are methods and the attributes have their respective types, int, str, dict, etc..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.