Displaying the Methods of an Object in MicroPython

MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimized to run on microcontrollers and constrained systems. When working with objects in MicroPython, it can be incredibly useful to know what methods an object has at your disposal. This blog post will explore different ways to display the methods of an object in MicroPython, covering fundamental concepts, usage methods, common practices, and best practices.

Table of Contents#

  1. Fundamental Concepts
  2. Usage Methods
  3. Common Practices
  4. Best Practices
  5. Conclusion
  6. References

Fundamental Concepts#

In Python and MicroPython, everything is an object. An object is an instance of a class, and a class can have attributes and methods. Attributes are variables associated with an object, while methods are functions that belong to an object. To display the methods of an object, we need to understand how to access the metadata of an object.

In MicroPython, the built - in dir() function is a key tool for this purpose. The dir() function returns a list of valid attributes and methods of an object. If no argument is passed to dir(), it returns the names in the current local scope.

Usage Methods#

Using the dir() function#

The dir() function is straightforward to use. Here is a simple example:

# Create a list object
my_list = [1, 2, 3]
 
# Use the dir() function to display attributes and methods
methods_and_attributes = dir(my_list)
 
# Print the result
for item in methods_and_attributes:
    print(item)

In this example, we create a list object my_list and then use the dir() function to get a list of all its attributes and methods. We then loop through the list and print each item.

Filtering for methods#

The dir() function returns both attributes and methods. To filter out only the methods, we can use the callable() function. A method is a callable object, so we can check if an item returned by dir() is callable.

# Create a list object
my_list = [1, 2, 3]
 
# Get all attributes and methods
all_items = dir(my_list)
 
# Filter for methods
methods = [item for item in all_items if callable(getattr(my_list, item))]
 
# Print the methods
for method in methods:
    print(method)

In this code, we first get all attributes and methods using dir(). Then, we use a list comprehension to filter out only the callable items using the callable() and getattr() functions. The getattr() function is used to get the value of an attribute or method of an object.

Common Practices#

Interactive Mode#

When working in the MicroPython REPL (Read - Evaluate - Print Loop), you can use the dir() function interactively to explore objects. For example, if you want to know what methods are available for the machine module in MicroPython, you can do the following:

import machine
print(dir(machine))

This will print all the attributes and methods of the machine module.

Documentation and Exploration#

Using dir() is a great way to quickly explore an object when you don't have access to detailed documentation. For example, if you are working with a new sensor module in MicroPython, you can use dir() to see what methods are available for interacting with the sensor.

Best Practices#

Error Handling#

When using getattr() to check for callable items, it's a good practice to add error handling. If an attribute or method does not exist, getattr() will raise an AttributeError. You can use a try - except block to handle this.

# Create a list object
my_list = [1, 2, 3]
 
# Get all attributes and methods
all_items = dir(my_list)
 
# Filter for methods with error handling
methods = []
for item in all_items:
    try:
        attr = getattr(my_list, item)
        if callable(attr):
            methods.append(item)
    except AttributeError:
        pass
 
# Print the methods
for method in methods:
    print(method)

Avoid Over - Reliance#

While dir() is a useful tool, it should not be the only way you learn about an object. Always refer to the official documentation for detailed information about an object's methods, their parameters, and return values.

Conclusion#

Displaying the methods of an object in MicroPython is an important skill for efficient programming. The dir() function is a powerful tool for getting a list of attributes and methods of an object. By combining it with the callable() and getattr() functions, you can filter out only the methods. Remember to use error handling and refer to official documentation for a more comprehensive understanding of the objects you are working with.

References#