Exploring built-in modules, the help() function

The interactive Python console is a good platform to explore built-in modules as well. Because Python comes equipped with two very useful functions, help() and dir(), you have instant access to a lot of information contained in Blender's (and Python's) modules as a lot of documentation is provided as part of the code.

For people not familiar with these functions, here are two short examples, both run from the interactive Python console. To get information on a specific object or function, type:

help(Blender.Lamp.Get)

The information will be printed in the same console:

Help on built-in function Get in module Blender.Lamp:
Lamp.Get (name = None):
Return the Lamp Data with the given name, None if not found, or
Return a list with all Lamp Data objects in the current scene,
if no argument was given.

The help() function will show the associated docstring of functions, classes, or modules. In the previous example, that is the information provided with the Get() method (function) of the Lamp class. A docstring is the first string defined in a function, class, or module. When defining your own functions, it is a good thing to do this as well. This might look like this:

def square(x):
"""
calculate the square of x.
"""
return x*x

We can now apply the help function to our newly-defined function like we did before:

help(square)

The output then shows:

Help on function square in module __main__:
square(x)
calculate the square of x.

In the programs that we will be developing, we will use this method of documenting where appropriate.

Exploring built-in functions, the dir() function

The dir() function lists all members of an object. That object can be an instance, but also a class or module. For example, we might apply it to the Blender.Lamp module:

dir(Blender.Lamp)

The output will be a list of all members of the Blender.Lamp module. You can spot the Get() function that we encountered earlier:

['ENERGY', 'Falloffs', 'Get', 'Modes', 'New', 'OFFSET', 'RGB', 'SIZE', 'SPOTSIZE', 'Types', '__doc__', '__name__', '__package__', 'get']

Once you know which members a class or module has, you can then check for any additional help information for these members by applying the help() function.

Of course both dir() and help() are most useful when you already have some clue where to look for information. But if so, they can be very convenient tools indeed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.198.94