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
How do I look inside a Python object?
Python provides several built-in functions that help you inspect and explore the internal structure of objects. These introspection tools are essential for debugging, learning, and understanding how objects work in Python.
Using the help() Function
The help() function provides comprehensive documentation about an object, including its methods, attributes, and usage examples ?
Example
from math import pow help(pow)
Help on built-in function pow in module math:
pow(x, y, /)
Return x**y (x to the power of y).
Using the dir() Function
The dir() function returns a list of all attributes and methods associated with an object, making it perfect for exploring what an object can do ?
Example
class Weight:
def __init__(self, kilos):
self.kilos = kilos
def to_pounds(self):
return self.kilos * 2.2048
# Get all attributes and methods of the Weight class
attributes = dir(Weight)
print("Custom methods:", [attr for attr in attributes if not attr.startswith('__')])
print("Total attributes:", len(attributes))
Custom methods: ['to_pounds'] Total attributes: 26
Using the type() Function
The type() function returns the exact type of an object, helping you understand what kind of data structure you're working with ?
Example
class Weight:
def __init__(self, kilos):
self.kilos = kilos
def to_pounds(self):
return self.kilos * 2.2048
# Check types of different elements
print("Class type:", type(Weight))
print("Method type:", type(Weight.to_pounds))
print("Instance type:", type(Weight(70)))
Class type: <class 'type'> Method type: <class 'function'> Instance type: <class '__main__.Weight'>
Using the id() Function
The id() function returns the unique memory address of an object, useful for understanding object identity ?
Example
class Weight:
def __init__(self, kilos):
self.kilos = kilos
# Create two instances
weight1 = Weight(70)
weight2 = Weight(70)
print("weight1 id:", id(weight1))
print("weight2 id:", id(weight2))
print("Same object?", weight1 is weight2)
weight1 id: 140234567890123 weight2 id: 140234567890456 Same object? False
Other Useful Functions
Python provides additional functions for object inspection ?
hasattr() and getattr()
class Weight:
def __init__(self, kilos):
self.kilos = kilos
def to_pounds(self):
return self.kilos * 2.2048
weight = Weight(70)
# Check if attribute exists
print("Has 'kilos' attribute:", hasattr(weight, 'kilos'))
print("Has 'grams' attribute:", hasattr(weight, 'grams'))
# Get attribute value
print("Kilos value:", getattr(weight, 'kilos'))
print("Default for missing attr:", getattr(weight, 'grams', 'Not found'))
Has 'kilos' attribute: True Has 'grams' attribute: False Kilos value: 70 Default for missing attr: Not found
callable() Function
class Weight:
def __init__(self, kilos):
self.kilos = kilos
def to_pounds(self):
return self.kilos * 2.2048
weight = Weight(70)
print("Is Weight callable?", callable(Weight))
print("Is to_pounds callable?", callable(weight.to_pounds))
print("Is kilos callable?", callable(weight.kilos))
Is Weight callable? True Is to_pounds callable? True Is kilos callable? False
Summary
| Function | Purpose | Returns |
|---|---|---|
help() |
Documentation | Help text |
dir() |
List attributes/methods | List of names |
type() |
Object type | Type object |
id() |
Memory address | Integer |
hasattr() |
Check attribute existence | Boolean |
getattr() |
Get attribute value | Attribute value |
Conclusion
These introspection functions are essential tools for debugging and exploring Python objects. Use dir() to discover available methods, help() for documentation, and type() to understand object types.
