Home Backend Development Python Tutorial Exploring Name Mangling in Python: What It Is and How It Works

Exploring Name Mangling in Python: What It Is and How It Works

Oct 09, 2024 pm 04:15 PM

Exploring Name Mangling in Python: What It Is and How It Works

Python is known for its simplicity and readability, but when it comes to object-oriented programming (OOP), there are some less-discussed mechanisms that are crucial for writing robust code. One such mechanism is name mangling. This article will guide you through what name mangling is, why Python uses it, and how it can help prevent name collisions in complex class hierarchies.

What Is Name Mangling

Python allows methods in classes to be overridden by subclasses. However, this can sometimes lead to name conflicts when a subclass unintentionally overrides attributes or methods from the parent class. Name mangling is a mechanism Python uses to avoid these conflicts, specifically for attributes that are meant to be private.

Name mangling in Python is a feature where the interpreter changes the names of private class properties in order to minimize the risk of them being accessed and overridden by mistake. This provides a level of privacy in class attributes, though it is not strictly enforced. However, it is not a strict enforcement.

Technical Definition

In Python, any identifier with two leading underscores (__) and no more than one trailing underscore will undergo name mangling. The interpreter transforms the name by prefixing it with the class name.

Why Does Python Use Name Mangling

In order to prevent naming conflicts, particularly in the situation when the subclasses may have their own variables that may override the variables in the parent class, Python implements name mangling. Name mangling addresses this problem.

from datetime import datetime, timedelta
from time import time, sleep

class Machine:
    def __init__(self, id):
        self.id = id
        self._started = time()

    def uptime(self):
        return time() - self._started

class PetrolMachine(Machine):
    def __init__(self, id):
        super().__init__(id)
        self._started = datetime.now()

    def cost(self):
        duration = datetime.now() - self._started
        return duration/timedelta(seconds=60) *0.02

worked = PetrolMachine('12345')
sleep(0.123)
print(f"uptime : {worked.uptime():.2f}")
Copy after login

In this example, the Machine class stores an ID and records the start time using Python’s time() function. When you request uptime, it calculates the difference between the current time and the start time, which is stored as a floating-point number. The subclass PetrolMachine, however, stores the start time using datetime.now(). When we try to calculate the uptime, the program throws an error because it expects start_time to be a floating-point number, but it's now a datetime object. This naming conflict can happen when subclass attributes unintentionally override parent class attributes. Name mangling helps avoid this issue.

How Does Name Mangling Works

So how does name mangling help resolve this issue? When a class attribute is prefixed with two underscores, Python internally changes the name to include the class name as a prefix. Here's how you can modify the Machine class to avoid name conflicts using name mangling:

We can resolve the error by applying name mangling to the __started attribute in the Machine class, as shown below:

from datetime import datetime, timedelta
from time import time, sleep

class Machine:
    def __init__(self, id):
        self.id = id
        self.__started = time()

    def uptime(self):
        return time() - self.__started  

class PetrolMachine(Machine):
    def __init__(self, id):
        super().__init__(id)
        self._started = datetime.now()

    def cost(self):
        duration = datetime.now() - self._started
        return duration/timedelta(seconds=60) *0.02


worked = PetrolMachine('12345')
sleep(0.123)
print(f"uptime : {worked.uptime():.2f}")
Copy after login

A simple way of expressing name mangling is shown below. I have a class ClassA, which have one private_variable, which is name mangled.

class MyClass:
    def __init__(self):
        self.__private_var = "I am private"

    def get_private_var(self):
        return self.__private_var

my_object = MyClass()
print(my_object.get_private_var())  # This works
print(my_object.__private_var)
Copy after login

The second print() will raise an AttributeError because the variable __private_var has been name-mangled. Internally, Python has changed the name to _MyClass__private_var, making it harder to access from outside the class.

Accessing The Mangled Name

While Python's name mangling is designed to prevent accidental access, it doesn’t enforce strict privacy. You can still access the mangled attribute by using the full mangled name, though this is discouraged. Here's how it works: my_object._MyClass__private_var

print(my_object._MyClass__private_var)
Copy after login

When Does Python Mangle Names

I will explain it through a simple example

class MyClass:
    def __init__(self):
        self._protected_var = "I'm protected"
        self.__private_var__ = "I'm not mangled"
Copy after login

In Python, a leading single underscore (e.g., _protected_var) indicates that the attribute is "protected" and should not be accessed directly from outside the class. However, Python does not enforce this. In contrast, names with two leading underscores (e.g., __private_var) are mangled to prevent accidental overriding. Importantly, names with double underscores on both sides (e.g., __special__) are not mangled and are reserved for special use cases such as magic methods.

優點和局限性

好處

  • 有助於防止意外覆蓋子類別中的類別屬性
  • 提供輕量級的隱私機制,但不會受到太多限制。

限制

  • 它並不是真正的私有——其他開發人員仍然可以存取損壞的名稱,如果他們知道如何的話。
  • 如果過度使用,會使偵錯和讀取程式碼變得更加複雜。

_ 儘管有這些限制,名稱修飾仍然是 Python OOP 工具包中的一個有用工具。雖然這不是嚴格的隱私保護,但它有助於防止命名衝突和意外的屬性覆蓋。了解名稱修飾將使您能夠編寫更健全、可維護的程式碼,尤其是在處理複雜的類別層次結構時。在您的專案中嘗試一下,並在下面的評論中分享您的經驗或問題! _

The above is the detailed content of Exploring Name Mangling in Python: What It Is and How It Works. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

See all articles