Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why set "private" python attribute from *outside* the class?

Tags:

python

django

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.

like image 299
Lauritz V. Thaulow Avatar asked Dec 03 '12 14:12

Lauritz V. Thaulow


People also ask

Why is it recommended to keep the class attributes private?

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.

Can we access private method from outside class Python?

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.

Why do we use private variables in Python?

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.

Should attributes be private Python?

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.


1 Answers

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

like image 162
dm03514 Avatar answered Oct 13 '22 00:10

dm03514