Home > Backend Development > Python Tutorial > What's the Difference Between Single and Double Underscores in Python Object Names?

What's the Difference Between Single and Double Underscores in Python Object Names?

DDD
Release: 2024-12-24 08:32:14
Original
603 people have browsed it

What's the Difference Between Single and Double Underscores in Python Object Names?

Demystifying Underscores in Python Object Names

In Python, naming conventions play a crucial role in object naming. Single and double underscores before an object's name hold specific meanings that guide programmers in understanding the scope and accessibility of that object.

Single Underscore (_name)

A single leading underscore in an object's name suggests that it's meant for internal use within the class. It's a way to indicate that the object is not intended to be accessed or modified from outside the class. However, it's important to note that this convention is a guideline rather than an enforced restriction.

Single underscores are also used to denote modules or functions that should not be imported from elsewhere. By prefixing the object's name with an underscore, it signifies to other programmers that it should be treated as a private element.

Double Underscore (__name__)

Double leading underscores are used for name mangling, a technique that modifies the object's name behind the scenes. This transformation replaces the object's name with a modified version that includes the class name and underscores.

According to Python's documentation, double underscores indicate that an object should be treated as a private variable or method within the current class. However, it's essential to understand that name mangling is not absolute. Determined individuals can still access or modify variables marked as private.

Example

Let's consider the following Python class:

class MyClass():
    def __init__(self):
        self.__superprivate = "Hello"
        self._semiprivate = ", world!"

mc = MyClass()
print(mc._semiprivate)  # Accessible
print(mc.__superprivate)  # Not accessible outside the class
Copy after login

In this example, the __superprivate variable is marked as private using double underscores, while the _semiprivate variable uses a single underscore. The __superprivate variable is inaccessible outside the class, while the _semiprivate variable can be accessed but should be considered internal to the class.

The above is the detailed content of What's the Difference Between Single and Double Underscores in Python Object Names?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template