Adventures in Machine Learning

Mastering Underscores: Key Concepts for Object-Oriented Programming in Python

Are you considering learning object-oriented programming in Python? If so, there are a few key concepts you should understand before diving in.

Object-oriented programming (OOP) is a programming paradigm that uses “objects” data structures consisting of data fields and associated functions to design programs.

One of the core principles of OOP is encapsulation, which involves packaging related data and functions together in a class.

This helps to hide the complexity of the implementation and allows for easier maintenance and modification in the future. Data members are the variables that hold data within a class, while member functions are the functions that manipulate that data.

The class is the blueprint that defines the structure and behavior of objects. Polymorphism is another important concept in OOP.

It involves creating multiple methods with the same name but different implementations. This provides a way to use a single interface to represent multiple related tasks.

The DRY (Don’t Repeat Yourself) principle is often invoked here, as it encourages the reuse of code whenever possible. Inheritance is another key OOP concept.

It allows for the creation of a new class (the child or derived class) that is a modified version of an existing class (the parent or base class). The child class inherits and extends the behavior of the parent class, allowing for more efficient code reuse.

Finally, abstraction is a core principle that allows us to expose only relevant details and hide implementation details. Access specifiers such as “public” and “private” help to control access to data members and member functions.

A leading underscore before a name conventionally indicates that it is intended to be private, while a double underscore before a name invokes name mangling to further protect the member. Achieving abstraction in Python specifically can be accomplished through various name nomenclature conventions.

Leading single underscores are used to indicate private members, discouraging external access to those members. Leading double underscores are used to invoke name mangling, which can make inheritance more straightforward.

Trailing single underscores can be used to avoid naming conflicts with Python’s built-in keywords. Finally, trailing double underscores are used for “dunder” methods, which are also known as “magic” methods and include __init__ for initializing objects and defining how objects behave in various data structures.

In conclusion, understanding the principles of OOP is crucial for anyone interested in creating robust, maintainable programs in Python. Encapsulation, polymorphism, inheritance, and abstraction are all essential concepts to master.

And, within Python specifically, leading single underscores, leading double underscores, trailing single underscores, and trailing double underscores are all conventions that can be used to achieve proper abstraction and ensure that your code is self-contained, modular, and reliable. By mastering these concepts, you’ll be well on your way to becoming a master coder in no time!

Python is known for being a highly versatile programming language, in part due to its flexible use of underscores in convention.

In this article, we’ll dive deeper into how leading single underscores, leading double underscores, trailing single underscores, and trailing double underscores can be used in Python programming. One common use case for leading single underscores is to indicate that a variable or method should not be accessed directly from outside the class.

This is often used when the inner workings of the class are complex or implementation-dependent. By adding a single underscore before a name, we indicate that it should be treated as private.

To access a private member of a class from outside the class, we can use a member function that is designed to retrieve and return the value of that private member. Here is an example:


class MyClass:

def __init__(self):

self._my_private_variable = 42

def get_private_variable(self):

return self._my_private_variable

my_obj = MyClass()

print(my_obj.get_private_variable()) # Output: 42

print(my_obj._my_private_variable) # Output: 42 (accessing variable directly)


In this example, we’ve created a MyClass object with a private variable called `_my_private_variable`.

To access this variable from outside the class, we’ve created a member function called `get_private_variable()` that returns the value of this variable. Note that we are not accessing the variable directly but instead using the function to retrieve it.

The use of leading double underscores, meanwhile, can help prevent inheritance ambiguity by name mangling, which means renaming variables and functions to avoid naming conflicts. This is often used when we want to avoid accidentally overwriting or accessing variables or functions from a parent class.

Here is an example of using leading double underscores:


class MyParentClass:

def __init__(self):

self.__my_private_variable = 42

class MyChildClass(MyParentClass):

def __init__(self):


self.__my_private_variable = 43

child_obj = MyChildClass()

print(child_obj._MyParentClass__my_private_variable) # Output: 42


In this example, we have defined a parent class with a private variable called `__my_private_variable`. We then define a child class that inherits from the parent class and redefines this variable to have a different value.

However, because we’ve used leading double underscores to name the variable, name mangling is invoked to ensure that the variable is unique to the child class and does not overwrite the parent class’s variable when we access it outside the class. To access it, we need to use the special name `_MyParentClass__my_private_variable`.

Moving on, adding a trailing single underscore to a variable name can be used to avoid naming collisions with Python’s built-in keywords, such as `print` or `list`. This is often used when using variable names that may conflict with existing variables or functions within a module or package.

Here’s an example:


list_ = [1, 2, 3]

print(list_) # Output: [1, 2, 3]


In this example, we’ve avoided the name collision with the built-in `list` keyword by adding a trailing underscore to our variable name. Finally, double underscores can also be used in the form of “magic methods” or “dunder” methods, which are special methods with names beginning and ending with double underscores.

These methods define how objects behave in certain operations, such as addition, comparison, or iteration.

Here are some examples of dunder methods in use:


class MyList:

def __init__(self, items):

self.items = items

def __len__(self):

return len(self.items)

def __contains__(self, value):

return value in self.items

my_list = MyList([1, 2, 3, 4])

print(len(my_list)) # Output: 4

print(2 in my_list) # Output: True


In this example, we’ve defined a class called `MyList` that takes a list of items as input.

We’ve then defined two dunder methods that define how `len()` and the `in` operator behave when applied to this class. In conclusion, underscores are used in Python conventions to help define variable and function scope, prevent namespace conflicts, and define class behavior.

When used effectively and judiciously, they can make code more legible, modular, and maintainable. In conclusion, underscores are a powerful tool for Python programmers to help define variable and function scope, prevent namespace conflicts, and define class behavior.

Leading single underscores denote private names, leading double underscores invoke name mangling for inheritance, trailing single underscores avoid naming conflicts with built-in keywords, and trailing double underscores are used for “dunder” methods to define class behavior in certain operations. By understanding and using these conventions, programmers can make their code more modular, maintainable, and readable.

Ultimately, mastering the use of underscores in Python coding can help accelerate progress and ensure success for any Python project.

Popular Posts