By singularli


2019-09-11 09:07:52 8 Comments

The doc says

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.

I am trying to get a better understanding about this procedure of Python code to be generated.

generated by what? the Python interpreter?

generated to what? the byte code?

is this figure apply this procedure?

enter image description here

can someone illustrate this procedure in more detail?

1 comments

@Ondrej K. 2019-09-11 10:49:04

You've made me curious, so you've made me look this up. First, when you look at the resulting byte code, it already uses STORE_NAME with the "mangled" name.

Who does it and when then? https://github.com/python/cpython/blob/master/Python/compile.c Holds the answer:

This file compiles an abstract syntax tree (AST) into Python bytecode.

And the corresponding function would be _Py_Mangle ("Name mangling: __private becomes _classname__private.")

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] How can you profile a Python script?

12 Answered Questions

[SOLVED] How can I make a time delay in Python?

31 Answered Questions

[SOLVED] How can I reverse a list in Python?

  • 2010-10-15 06:59:39
  • Leo.peis
  • 1326259 View
  • 920 Score
  • 31 Answer
  • Tags:   python list

11 Answered Questions

[SOLVED] Can someone explain __all__ in Python?

10 Answered Questions

[SOLVED] Python name mangling

43 Answered Questions

[SOLVED] How can I represent an 'Enum' in Python?

1 Answered Questions

[SOLVED] Underscores Problem in Name Mangling from docs Private Variables

11 Answered Questions

[SOLVED] What are "named tuples" in Python?

1 Answered Questions

[SOLVED] Best way for a class to access a private method (private name mangling)

  • 2014-08-20 16:00:49
  • Chrispresso
  • 191 View
  • 3 Score
  • 1 Answer
  • Tags:   python

Sponsored Content