Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Is there any Python object inspector?
Python doesn't have a built-in object inspector, but it provides several powerful functions and modules for examining objects. Functions like type(), help(), dir(), vars(), and the inspect module help you discover attributes, properties, and methods of any object.
Additional functions like id(), getattr(), hasattr(), globals(), locals(), and callable() are useful for examining object internals. Let's explore these inspection techniques using a simple class example.
Sample Class for Demonstration
First, let's create a Height class that we'll use throughout our examples ?
class Height:
"""
A height class that describes height in inches.
"""
def __init__(self, inches):
"""
Assign the amount of inches to the height object
"""
self.inches = inches
def to_centimeter(self):
"""
Convert the inches to centimeters.
1 inch = 2.54 centimeter
"""
return self.inches * 2.54
Using type() Function
The type() function returns the exact type of an object ?
Syntax
type(object)
Example
class Height:
"""
A height class that describes height in inches.
"""
def __init__(self, inches):
self.inches = inches
def to_centimeter(self):
return self.inches * 2.54
h = Height(2)
print("Type of h:", type(h))
print("Type of Height:", type(Height))
Type of h: <class '__main__.Height'> Type of Height: <class 'type'>
Using help() Function
The help() function provides detailed documentation about an object, including methods and docstrings ?
class Height:
"""
A height class that describes height in inches.
"""
def __init__(self, inches):
"""
Assign the amount of inches to the height object
"""
self.inches = inches
def to_centimeter(self):
"""
Convert the inches to centimeters.
1 inch = 2.54 centimeter
"""
return self.inches * 2.54
help(Height)
Help on class Height in module __main__: class Height(builtins.object) | Height(inches) | | A height class that describes height in inches. | | Methods defined here: | | __init__(self, inches) | Assign the amount of inches to the height object | | to_centimeter(self) | Convert the inches to centimeters. | 1 inch = 2.54 centimeter | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)
Using dir() Function
The dir() function returns a list of all attributes and methods available for an object ?
class Height:
def __init__(self, inches):
self.inches = inches
def to_centimeter(self):
return self.inches * 2.54
h = Height(24)
attributes = dir(h)
print("Total attributes:", len(attributes))
print("Custom method:", 'to_centimeter' in attributes)
print("Instance variable:", 'inches' in dir(h))
Total attributes: 27 Custom method: True Instance variable: False
Using hasattr() Function
The hasattr() function checks if an object has a specific attribute ?
Syntax
hasattr(object, name)
Example
class Height:
def __init__(self, inches):
self.inches = inches
def to_centimeter(self):
return self.inches * 2.54
h = Height(24)
print("Has 'to_centimeter' method:", hasattr(h, "to_centimeter"))
print("Has 'inches' attribute:", hasattr(h, "inches"))
print("Has 'weight' attribute:", hasattr(h, "weight"))
Has 'to_centimeter' method: True Has 'inches' attribute: True Has 'weight' attribute: False
Using vars() Function
The vars() function returns the __dict__ attribute of an object, showing instance variables ?
class Height:
def __init__(self, inches):
self.inches = inches
self.unit = "inches"
def to_centimeter(self):
return self.inches * 2.54
h = Height(24)
print("Instance variables:", vars(h))
Instance variables: {'inches': 24, 'unit': 'inches'}
Comparison of Inspection Methods
| Function | Purpose | Returns |
|---|---|---|
type() |
Get object type | Class type |
help() |
Get documentation | Formatted help text |
dir() |
List all attributes | List of attribute names |
hasattr() |
Check attribute existence | Boolean (True/False) |
vars() |
Get instance variables | Dictionary of variables |
Conclusion
Python provides excellent object inspection capabilities through built-in functions like type(), help(), dir(), hasattr(), and vars(). These functions help developers understand object structure, available methods, and attributes during development and debugging.
