How to find out what methods, properties, etc a python module possesses

Question:

Lets say I import a module. In order for me to make the best use of it, I would like to know what properties, methods, etc. that I can use. Is there a way to find that out?

As an example: Determining running programs in Python

In this line:

os.system('WMIC /OUTPUT:C:ProcessList.txt PROCESS get Caption,Commandline,Processid')

Let’s say I wanted to also print out the memory consumed by the processes. How do I find out if that’s possible? And what would be the correct ‘label’ for it? (just as the author uses ‘Commandline’, ‘ProcessId’)

Similarly, in this:

import win32com.client
def find_process(name):
    objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    objSWbemServices = objWMIService.ConnectServer(".", "rootcimv2")
    colItems = objSWbemServices.ExecQuery(
         "Select * from Win32_Process where Caption = '{0}'".format(name))
    return len(colItems)

print find_process("SciTE.exe")

How would I make the function also print out the memory consumed, the executable path, etc.?

Asked By: ldmvcd

||

Answers:

Python has a build in function called dir(). I’m not sure if this is what you are referring to, but fire up a interactive python console and type:

import datetime
dir(datetime)

This should give you a list of methods, properties and submodules

Answered By: Dirk

As for Python modules, you can do

>>> import module
>>> help(module)

and you’ll get a list of supported methods (more exactly, you get the docstring, which might not contain every single method). If you want that, you can use

>>> dir(module)

although now you’d just get a long list of all properties, methods, classes etc. in that module.

In your first example, you’re calling an external program, though. Of course Python has no idea which features wmic.exe has. How should it?

Answered By: Tim Pietzcker

dir(module) returns the names of the attributes of the module

module.__dict__ is the mapping between the keys and the attributes objects themselves

module.__dict__.keys() and dir(module) are lists having the same elements, though they are not equals because the elements aren’t in same order in them

it seems that help(module) iswhat you really need

Answered By: eyquem

@ldmvcd

Ok, excuse me, I think you are a beginner and you don’t see to what fundamental notions I am refering.

Objects are Python’s abstraction for
data. All data in a Python program is
represented by objects or by relations
between objects.
http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy

I don’t understand why it is called “abstraction”: for me an object is something real in the machine, a series of bits organized according certain rules to represent conceptual data or functionning.

Names refer to objects. Names are
introduced by name binding operations.
Each occurrence of a name in the
program text refers to the binding of
that name established in the innermost
function block containing the use.
http://docs.python.org/reference/executionmodel.html#naming-and-binding

.

A namespace is a mapping from names to
objects. Most namespaces are currently
implemented as Python dictionaries
,
but that’s normally not noticeable in
any way (except for performance), and
it may change in the future. Examples
of namespaces are: the set of built-in
names (containing functions such as
abs(), and built-in exception names);
the global names in a module; and the
local names in a function invocation.
In a sense the set of attributes of an
object also form a namespace.
http://docs.python.org/tutorial/classes.html#a-word-about-names-and-objects

.

By the way, I use the word attribute
for any name following a dot — for
example, in the expression z.real,
real is an attribute of the object z.
Strictly speaking, references to names
in modules are attribute references:
in the expression modname.funcname,
modname is a module object and
funcname is an attribute of it. In
this case there happens to be a
straightforward mapping between the
module’s attributes and the global
names defined in the module: they
share the same namespace!
http://docs.python.org/tutorial/classes.html#a-word-about-names-and-objects

.

Namespaces are created at different
moments and have different lifetimes.
http://docs.python.org/tutorial/classes.html#a-word-about-names-and-objects

.

The namespace for a module is
automatically created the first time a
module is imported. The main module
for a script is always called
main. http://docs.python.org/reference/executionmodel.html#naming-and-binding

.

Well, a Python programm is a big machine that plays with objects, references to these objects , names of these objects, and namespaces in which are binded the names and the objects , namespaces being implemented as dictionaries.

So, you’re right: when I refer to keys , I refer to names being the keys in the diverse namespaces. Names are arbitrary or not , according if the objects they have been created to name are user’s objects or built-in objects.

I give advise you to read thoroughly the parts

3.1. Objects , values and types
http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy

and

4.1. Naming and binding
http://docs.python.org/reference/executionmodel.html#naming-and-binding

Answered By: eyquem
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.