A Python code can ask and answer questions about itself and the objects it is manipulating.
`dir()` is a built-in function which returns a list of all the names belonging to some namespace.
If no arguments are passed to dir (i.e. `dir()`), it inspects the namespace in which it was called.
If `dir` is given an argument (i.e. `dir(, then it inspects the namespace of the object which it was passed.
For example:
apples = ['Cox', 'Braeburn', 'Jazz']
dir(apples)
dir()
name = "Peter"
dir(name)
You will find many names which both start and end with a double underscore (e.g. `__name__`). These are called magic names. Functions with magic names provide the implementation of particular python functionality.
For example, the application of the `str` to an object `a`, i.e. `str(a)`, will – internally – result in the method `a.__str__()` being called. This method `__str__` generally needs to return a string. The idea is that the `__str__()` method should be defined for all objects (including those that derive from new classes that a programmer may create) so that all objects (independent of their type or class) can be printed using the `str()` function. The actual conversion of some object `x` to the string is then done via the object specific method `x.__str__()`.
We can demonstrate this by creating a class `my_int` which inherits from the Python’s integer base class, and overrides the `__str__` method. (It requires more Python knowledge than provided up to this point in the text to be able to understand this example.)
class my_int(int):
"""Inherited from int"""
def __str__(self):
"""Tailored str representation of my int"""
return "my_int: %s" % (int.__str__(self))
a = my_int(3)
b = int(4) # equivalent to b = 4
print("a * b = ", a * b)
print("Type a = ", type(a), "str(a) = ", str(a))
print("Type b = ", type(b), "str(b) = ", str(b))
See →Python documentation, Data Model
The `type( command returns the type of an object:
type(1)
type(1.0)
type("Python")
import math
type(math)
type(math.sin)
`isinstance( returns True if the given object is an instance of the given type, or any of its superclasses. Use `help(isinstance`) for the full syntax.
isinstance(2,int)
isinstance(2.,int)
isinstance(a,int) # a is an instance of my_int
type(a)
The `help( function will report the docstring (magic attritube with name `__doc__`) of the object that it is given, sometimes complemented with additional information. In the case of functions, `help` will also show the list of arguments that the function accepts (but it cannot provide the return value).
`help()` starts an interactive help environment.
It is common to use the `help` command a lot to remind oneself of the syntax and semantic of commands.
help(isinstance)
import math
help(math.sin)
help(math)
The `help` function needs to be given the name of an object (which must exist in the current name space). For example pyhelp(math.sqrt) will not work if the `math` module has not been imported before
help(math.sqrt)
import math
help(math.sqrt)
Instead of importing the module, we could also have given the string of `math.sqrt` to the help function, i.e.:
help('math.sqrt')
`help` is a function which gives information about the object which is passed as its argument. Most things in Python (classes, functions, modules, etc.) are objects, and therefor can be passed to help. There are, however, some things on which you might like to ask for help, which are not existing Python objects. In such cases it is often possible to pass a string containing the name of the thing or concept to help, for example
`help(’modules’)` will generate a list of all modules which can be imported into the current interpreter. Note that help(modules) (note absence of quotes) will result in a NameError (unless you are unlucky enough to have a variable called modules floating around, in which case you will get help on whatever that variable happens to refer to.)
`help(’some_module’)`, where some_module is a module which has not been imported yet (and therefor isn’t an object yet), will give you that module’s help information.
`help(’some_keyword’)`: For example `and`, `if` or `print` (i.e. `help(’and’)`, `help(’if’)` and `help(’print’)`). These are special words recognized by Python: they are not objects and thus cannot be passed as arguments to help. Passing the name of the keyword as a string to help works, but only if you have Python’s HTML documentation installed, and the interpreter has been made aware of its location by setting the environment variable PYTHONDOCS.
The command `help( accesses the documentation strings of objects.
Any literal string apparing as the first item in the definition of a class, function, method or module, is taken to be its docstring.
`help` includes the docstring in the information it displays about the object.
In addition to the docstring it may display some other information, for example, in the case of functions, it displays the function’s signature.
The docstring is stored in the object’s `__doc__` attribute.
help(math.sin)
print(math.sin.__doc__)
For user-defined functions, classes, types, modules, …), one should always provide a docstring.
Documenting a user-provided function:
def power2and3(x):
"""Returns the tuple (x**2, x**3)"""
return x**2 ,x**3
power2and3(2)
power2and3(4.5)
power2and3(0+1j)
help(power2and3)
print(power2and3.__doc__)