However, it is still visible: > f = Foo()Ī variable name starting with two underscores is still public, but it is name-mangled and thus harder to access: > f._zĪttributeError: 'Foo' object has no attribute '_z' In python practice, a variable name starting with an underscore is "internal" and not part of the class interface, and so programmers should not rely on it. The programmer "activates" it by putting two underscores at the start of the variable name.įor example, I can define a simple class with some members: > class Foo(object): In python, name-mangling is a system by which class variables have different names inside and outside the class. That's the whole reason and purpose for name mangling.) (Admittedly this example is reaching, but as a compiler writer, I have to guarantee that distinct identifiers in the source code map to distinct link-time names. If an identifier is used in more than one module or namespace, join the name of the module with the name of the identifier, e.g., List_get instead of List.get.ĭepending on what characters are legal in link-time names, you may have to do additional mangling for example, it may be necessary to use the underscore as an 'escape' character, so you can distinguish Common in C++ (especially easy since overloading is permitted only for functions/methods and only on argument types) and Ada (where you can overload result types as well). Map each type to a string and use the combined high-level identifier and "type string" as the link-time name. Multiple types and multiple modules cover the usual contexts. In Ada or Modula-3, function get may appear in multiple modules. In C++, function or method get may be overloaded at multiple types. (An identifier is "overloaded" if the same name is used in more than one context or with more than one meaning.) Name mangling solves the problem of overloaded identifiers in programming languages. In the programming language of your choice, if an identifier is exported from a separately compiled unit, it needs a name by which it is known at link time.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |