Python map function, passing by reference/value?

前端 未结 3 1983
天命终不由人
天命终不由人 2021-01-12 11:23

I have a question about the map function in Python.

From what I understand, the function does not mutate the list it\'s operating on, but rather create

3条回答
  •  情深已故
    2021-01-12 11:40

    The data model of Python is based on a trilogy:
    identifier - reference - object

    .

    • The identifier is a string written in the code.
    • The reference is a variable stricto sensu, that is to say "a chunk of memory whose content can change". The value of a reference is the adress of the object.
    • The object has an implementation based on the structures of the langage C that is the foundations of Python.

    Other words are also used to designate the 'identifier' :
    1) name
    2) variable ; because this word is used by metonymy in mathematics to designate the symbols that represent the real mathematical variables, and the variables in a computer have conceptually the same functioning as the mathematical variables (their values can change).
    This use in Python is a very bad habit in my opinion : it creates ambiguities and confusion with what is called 'variable' in computer science: "chunk of memory whose content can change".

    The best is to use the word : identifier

    .

    An identifier and an object are binded in a certain namespace. Namespaces are displayed under the form of Python's dictionaries, but they ARE NOT dictionaries.

    • The binding of the identifier and the object is indirect, via the reference.

    • The binding of the identifier and the reference is direct, and realized in the SYMBOL TABLE (or symbol-table).

    In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is associated with information relating to its declaration or appearance in the source, such as its type, scope level and sometimes its location.
    http://en.wikipedia.org/wiki/Symbol_table

    They say: identifiers. Precisely.
    I rarely see allusions to symbol table, though it's the crucial thing that sheds light on the functioning of the Python's data model IMO.

    In my opinion, the word

    binding

    doesn't designate a precise and unique mechanism but globally the set of all the mechanisms concerning the trilogy identifier - reference - object

    .

    I dont' pretend that I perfectly understood all the matters concerning the data and execution models of Python, and that the above considerations are the more exact and precisely expressed that can be done.
    However, they allow me to have an operational understanding of what happens during executions of code.
    I would be very happy to be corrected on some points if I am wrong (for exemple, I am very satisfied to have learnt from Michael Foord that the nature of namespaces is not being dictionary, which is only the way they are represented)

    .

    That said, I don't know what is called value and reference when the subject of passing something as argument in Python is discussed, and I have the impression that a lot of the persons that have expressed on the subject in numerous esoteric discussions don't know more than me. I think that there is no best and lucid opinion on the subject that this one of Alex Martelli:

    "Trying to reuse terminology that is more generally applied to languages where "variables are boxes" to a language where "variables are post-it tags" is, IMHO, more likely to confuse than to help."

    Alex Martelli

    http://bytes.com/topic/python/answers/37219-value-reference

提交回复
热议问题