In asking a question about reflection I asked:
Nice answer. But there is a difference between saying
myobject.foo()
andx = getattr(myobject, "foo"); x();
. Even if it is only cosmetic. In the first the foo() is compiled in statically. In the second, the string could be produced any number of ways. – Joe 1 hour ago
Which got the answer:
Eh, potato / solanum tuberosum... in python, niether is statically compiled, so they are more or less equivalent. – SWeko 1 hour ago
I know that Python objects' members are stored in a dictionary and that everything is dynamic, but I assumed that given the following code:
class Thing():
def m(self):
pass
t = Thing()
The following code would somehow get statically compiled when the .pyc is generated:
t.m()
i.e. the compiler knows the address of m()
, so no point binding at runtime. That or the runtime would cache subsequent lookups.
Whereas this would always involve hitting the dictionary:
meth = getattr(t, "m")
meth()
Are all invocations treated as string lookups in dictionaries? Or are the two examples actually identical?