Polymorphism :
Polymorphism is a foundational concept in programming that allows entitles like functions, methods or operators to behave differently based on the type of data they are handling. Polymorphism is one of the core concepts in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It enables flexibility and reusability in code.
Polymorphism basic built-in-functions:
The basic built-in functions exhibit polymorphism, adapting to various data types.
print(len("Hello"))
print(len([1, 2, 3]))
print(max(1, 3, 2))
print(max("a", "z", "m"))
Polymorphism means "many forms." In Python, it allows methods to behave differently based on the object that calls them. For example, the +
operator can add numbers, concatenate strings, or merge lists, depending on the context.
Doctyping and Docstrings'
Doctyping
Doctyping refers to the practice of adding type hints and documentation to your code to make it more readable and maintainable. Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python, Modules, functions, classes, and methods. It’s specified in source code that is used, like a comment, to document a specific segment of code.
def multiply_numbers(a, b):
"""
Multiplies two numbers and returns the result.
Args:
a (int): The first number.
b (int): The second number.
Returns:
int: The product of a and b.
"""
return a * b
print(multiply_numbers(3,5))
Google style docstrings follow a specific format and are inspired by Google’s documentation style guide. They provide a structured way to document Python code, including parameters, return values, and descriptions.
Docstring
def my_function():
'''Demonstrates triple double quotes
docstrings and does nothing really.'''
return None
print("Using __doc__:")
print(my_function.__doc__)
A docstring is a string literal that appears right after the definition of a function, class, or module. It is used to describe what the function, class, or module does. Docstrings are accessible using the __doc__
attribute
Using __doc__:
Demonstrates triple double quotes
docstrings and does nothing really.
The function
my_function()
has a docstring, which is a multi-line string enclosed in triple double quotes ("""
).print(my_function.__doc__)
prints the docstring of the function.
Key Functions:
The
__doc__
attribute is used to retrieve the docstring of a function.Docstrings provide documentation and should be enclosed in triple double quotes (
"""
) or triple single quotes ('''
).They are commonly used to describe what a function does.
In Python, magic methods (or dunder methods) are special methods that start and end with double underscores. The __doc__
attribute is one such magic method that stores the docstring of a function, class, or module.
The difference between a Docstring and a Comment
Comments*: Comments in Python start with a hash mark (*
#
) and are intended to explain the code to developers. They are ignored by the Python interpreter.Docstrings*: Docstrings provide a description of the function, method, class, or module. Unlike comments, they are not ignored by the interpreter and can be accessed at runtime using the*
.__doc__
attribute.
How to Access a Parent Class with __base__
In python you can access the parent class of a class using the __base__
attributes. This is useful for understanding inheritance
class Animal:
"""A class representing an animal."""
pass
class Dog(Animal):
"""A class representing a dog."""
pass
print(Dog.__base__) # Output: <class '__main__.Animal'>
Conclusion:
Polymorphism is a powerful concept in Python that allows for flexible and reusable code. By combining it with docstrings, doctyping, and magic methods like __doc__
, you can write clean, well-documented, and maintainable code. Additionally, understanding how to access parent classes using __base__
helps you navigate inheritance hierarchies effectively.
I hope this blog post has given you a clear understanding of these concepts. Feel free to experiment with the examples provided and explore further!