Reading the Django source code I came upon this function. It's the implementation for the block tag.
What interests me is that they're setting a variable with two leading underscores (__loaded_blocks
) from outside the parser
class instance (parser is an instance of the Parser class). A quick grep in the Django source code shows that the string loaded_blocks
occurs only here.
Now I've never considered this use of the python name-mangling feature before, but this will in effect hide the __loaded_blocks
attribute of parser
from itself! To read this attribute from a parser
method you have to resort to getattr(self, "__loaded_blocks")
.
Am I right in thinking this is just an unintended and unused side effect of the chosen attribute name? Or is there a deeper purpose to this?
In general, why would you want to do such a thing?
EDIT: To clarify, I'm fully aware that as long as you don't try to access the __loaded_blocks
attribute from a method of parser
, it will work just like any other attribute, and that it is in fact not a mangled attribute.
By defining the class members' private , we can protect the data from accidental corruption. The following are the benefits of encapsulation: Protection of data from accidental corruption. Specification of the accessibility of each of the members of a class to the code outside the class.
Private methods are those methods that should neither be accessed outside the class nor by any base class. In Python, there is no existence of Private methods that cannot be accessed except inside a class.
The mangling rules are designed mostly to avoid accidents but it is still possible to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger.
Private attributes are essential attributes of a class that can only be accessed within the class itself. If you have worked with languages like Java before, you might know that private attributes are made private using the private keyword. However, Python has no private keyword.
I don't think Name mangling will take place when you add a property prefixed with __
to an instance
from the docs:
Private name mangling: When an identifier that textually occurs in a class definition begins with two or more underscore characters and does not end in two or more underscores, it is considered a private name of that class. Private names are transformed to a longer form before code is generated for them. The transformation inserts the class name in front of the name, with leading underscores removed, and a single underscore inserted in front of the class name. For example, the identifier __spam occurring in a class named Ham will be transformed to Ham_spam. This transformation is independent of the syntactical context in which the identifier is used. If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may happen. If the class name consists only of underscores, no transformation is done.
class Test:
pass
test = Test()
test.__hello = 'hii'
test.__hello # hiii
Although the name is not mangled it still marks this as "private" to the consumer of the code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With