How the method resolution and invocation works internally in Python?

前端 未结 3 1198
无人共我
无人共我 2020-12-10 17:33

How the methods invocation works in Python? I mean, how the python virtual machine interpret it.

It\'s true that the python method resolution could be slower in Pyth

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 18:18

    It's true that the python method resolution could be slower in Python that in Java. What is late binding?

    Late binding describes a strategy of how an interpreter or compiler of a particular language decides how to map an identifier to a piece of code. For example, consider writing obj.Foo() in C#. When you compile this, the compiler tries to find the referenced object and insert a reference to the location of the Foo method that will be invoked at runtime. All of this method resolution happens at compile time; we say that names are bound "early".

    By contrast, Python binds names "late". Method resolution happens at run time: the interpreter simply tries to find the referenced Foo method with the right signature, and if it's not there, a runtime error occurs.

    What are the differences on the reflection mechanism in these two languages?

    Dynamic languages tend to have better reflection facilities than static languages, and Python is very powerful in this respect. Still, Java has pretty extensive ways to get at the internals of classes and methods. Nevertheless, you can't get around the verbosity of Java; you'll write much more code to do the same thing in Java than you would in Python. See the java.lang.reflect API.

提交回复
热议问题