diff --git a/content/python/concepts/built-in-functions/terms/getattr/getattr.md b/content/python/concepts/built-in-functions/terms/getattr/getattr.md index 02f5398e8f0..006720ca023 100644 --- a/content/python/concepts/built-in-functions/terms/getattr/getattr.md +++ b/content/python/concepts/built-in-functions/terms/getattr/getattr.md @@ -1,105 +1,163 @@ --- Title: 'getattr()' -Description: 'Returns the value of the named property in the specified object.' +Description: 'Returns the value of a named attribute from an object.' Subjects: - 'Computer Science' - 'Data Science' Tags: + - 'Attributes' - 'Functions' - - 'Methods' - 'Objects' - - 'Classes' - - 'OOP' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -The built-in **getattr()** function returns the value of the named property in a specified object/[Class](https://github.com/Codecademy/docs/blob/main/content/python/concepts/classes/classes.md). +The **`getattr()`** function is a built-in Python function that returns the value of a named attribute from an object. It provides a dynamic way to access object attributes using their [string](https://www.codecademy.com/resources/docs/python/strings) names rather than dot notation. This function is particularly useful when the attribute name is stored in a variable or determined at runtime. + +The `getattr()` function serves as a safe alternative to direct attribute access, allowing developers to specify a default value when an attribute doesn't exist. This makes it invaluable for creating flexible code that can handle objects with varying attributes, implementing configuration systems, and building dynamic applications where attribute names may not be known in advance. ## Syntax -```shell +```pseudo getattr(object, name, default) ``` -The `getattr()` method takes at least 2 parameters: +**Parameters:** + +- `object`: The object whose attribute needs to be accessed +- `name`: A string containing the name of the attribute to retrieve +- `default` (optional): The value to return if the specified attribute is not found + +**Return value:** -- `object`, the given object -- `name`, a string with the name of the attribute -- `default`, (optional) a value to be returned when the named attribute does not exist +The value of the specified attribute if it exists, the default value if the attribute doesn't exist and a default is provided, or raises an `AttributeError` if the attribute doesn't exist and no default is specified. -## Examples +## Example 1: Basic Attribute Access -### Example 1 +This example demonstrates the fundamental usage of `getattr()` to access object attributes dynamically: ```py -class Cat: - hobby = 'sleeping' - age = 2 +class Student: + def __init__(self, name, age, grade): + self.name = name + self.age = age + self.grade = grade -pet = Cat() -attr = getattr(pet, 'age') -print("My cat is", attr, "years old.") +# Create a student object +student = Student("Alice", 20, "A") + +# Access attributes using getattr() +student_name = getattr(student, "name") +student_age = getattr(student, "age") + +print(f"Student name: {student_name}") +print(f"Student age: {student_age}") ``` -This code will return: +This example results in the following output: ```shell -My cat is 2 years old. +Student name: Alice +Student age: 20 ``` -### Example 2 +## Example 2: Configuration Management System -```py -class Cat: - hobby = 'sleeping' - age = 2 +This example shows how `getattr()` can be used in a real-world scenario for managing application configurations with default values: -pet = Cat() -attr = getattr(pet, 'love') -print("My cat's favourite food is", attr) +```py +class AppConfig: + def __init__(self): + self.database_url = "localhost:5432" + self.debug_mode = True + self.cache_timeout = 300 + +def get_config_value(config, setting_name, default_value=None): + """Safely retrieve configuration values with fallback defaults""" + return getattr(config, setting_name, default_value) + +# Create configuration object +config = AppConfig() + +# Retrieve existing and non-existing configuration values +db_url = get_config_value(config, "database_url", "default.db") +api_key = get_config_value(config, "api_key", "not_configured") +max_connections = get_config_value(config, "max_connections", 100) + +print(f"Database URL: {db_url}") +print(f"API Key: {api_key}") +print(f"Max Connections: {max_connections}") ``` -This code will output an error, this because the `Cat` object does not have an attribute named `love`, and we did not set `default` parameter: +This example results in the following output: ```shell -Traceback (most recent call last): - File "main.py", line 6, in - attr = getattr(pet, 'love') -AttributeError: 'Cat' object has no attribute 'love' +Database URL: localhost:5432 +API Key: not_configured +Max Connections: 100 ``` -### Example 3 +## Codebyte Example: Dynamic Method Execution -```py -class Cat: - hobby = 'sleeping' - age = 2 +This example illustrates using `getattr()` to dynamically call methods based on user input or runtime conditions: -pet = Cat() -attr = getattr(pet, 'love', 'tuna') -print("My cat's favourite food is", attr) -``` +```codebyte/python +class Calculator: + def add(self, x, y): + return x + y -This code will return: + def subtract(self, x, y): + return x - y -```shell -My cat's favourite food is tuna -``` + def multiply(self, x, y): + return x * y -## Codebyte Example + def divide(self, x, y): + if y != 0: + return x / y + return "Cannot divide by zero" -Use `getattr()` to get the value of the named property in a specified object/class: +def execute_operation(calculator, operation_name, x, y): + """Execute mathematical operation based on operation name""" + # Get the method using getattr with a default fallback + operation_method = getattr(calculator, operation_name, None) -```codebyte/python -class Cat: - hobby = 'sleeping' - age = 2 - -pet = Cat() -attr_1= getattr(pet, 'hobby') -attr_2 = getattr(pet, 'love', 'tuna') -print("My cat's hobby is", attr_1) -print("My cat's favourite food is", attr_2) + if operation_method and callable(operation_method): + return operation_method(x, y) + else: + return f"Operation '{operation_name}' not supported" + +# Create calculator instance +calc = Calculator() + +# Execute different operations dynamically +operations = ["add", "multiply", "modulo"] +x, y = 10, 3 + +for op in operations: + result = execute_operation(calc, op, x, y) + print(f"{x} {op} {y} = {result}") ``` + +## Frequently Asked Questions + +### 1. What happens if I don't provide a default value and the attribute doesn't exist? + +Python will raise an `AttributeError` exception. Always use a default value or handle the exception appropriately. + +### 2. Can I use `getattr()` to access private attributes? + +Yes, `getattr()` can access private attributes (those starting with underscore), but this goes against Python conventions and should be avoided. + +### 3. Is `getattr()` slower than direct attribute access? + +Yes, `getattr()` has slightly more overhead than direct dot notation access, but the performance difference is typically negligible for most applications. + +### 4. Can I use `getattr()` with methods? + +Yes, `getattr()` returns method objects that can be called. Always check if the returned object is callable using `callable()` before invoking it. + +### 5. What's the difference between `getattr()` and `hasattr()`? + +`hasattr()` checks if an attribute exists and returns `True` or `False`, while `getattr()` retrieves the actual value of the attribute.