0% completed
Reflection is a powerful feature in programming that allows a program to manipulate the properties and capabilities of objects at runtime. In Python, reflection enables the inspection of classes, modules, and functions while the program is running, allowing you to examine and modify their behavior dynamically.
Python provides several built-in functions that support reflective capabilities, such as getattr()
, setattr()
, hasattr()
, and delattr()
. These functions allow for dynamic manipulation of attributes, and the type()
and isinstance()
functions help in introspecting types.
The getattr()
function allows you to access the value of an object's attribute by name.
In this example, we will retrieve an attribute from an object using getattr()
.
Explanation:
class Person:
Defines a class with two attributes: name
and age
.getattr(person, 'name', 'Attribute not found')
: Retrieves the value of the name
attribute from the person
object. If the attribute does not exist, it returns 'Attribute not found'.The setattr()
function allows you to set the value of an attribute of an object.
In this example, we will set an attribute of an object using setattr()
.
Explanation:
setattr(person, 'age', 35)
: Sets the age
attribute of the person
object to 35.print(person.age)
: Displays the updated age, verifying that the attribute's value has been changed.The hasattr()
function checks if an object has an attribute.
In this example, we will check for the presence of an attribute in an object using hasattr()
.
Explanation:
hasattr(person, 'name')
: Checks if the person
object has an attribute named name
.print(has_attribute)
: Outputs True
indicating that the attribute exists.The delattr()
function deletes an attribute from an object.
In this example, we will delete an attribute from an object using delattr()
.
Explanation:
delattr(person, 'age')
: Deletes the age
attribute from the person
object.print(hasattr(person, 'age'))
: After deletion, this check returns False
, indicating that the age
attribute no longer exists.The type()
function is essential for identifying the type of an object, which can be critical in debugging and dynamic type handling in applications.
In this example, we will demonstrate how to use type()
to obtain the type of various objects.
Explanation:
type(5)
: Returns the type of an integer, which is <class 'int'>
.type("Hello")
: Returns the type of a string, which is <class 'str'>
.type(person)
: Returns the type of person
, which is <class '__main__.Person'>
. This indicates that person
is an instance of the Person
class defined in the current module.isinstance()
is used to check if an object is an instance of a specific class or a tuple of classes, which is particularly useful in polymorphism where you want to confirm an object's class in inheritance hierarchies.
In this example, we will check if certain objects are instances of predefined classes.
Explanation:
isinstance(person, Person)
: Checks whether person
is an instance of the Person
class.isinstance(person, dict)
: Checks whether person
is an instance of the dict
class. Since person
is a Person
instance, this returns False
.isinstance("Hello", (str, int))
: Checks whether "Hello" is an instance of either the str
or int
class. Since it is a string, this returns True
.Reflection is a valuable feature in Python that enhances the flexibility of the code by allowing dynamic manipulation of objects. By using reflection methods, developers can write more generic and versatile functions, making the code easier to maintain and extend. This lesson on Python reflection provides foundational knowledge for using dynamic features to inspect and manipulate objects programmatically, fostering a deeper understanding of Python's capabilities.
.....
.....
.....