问题
Eclipse is able to utilize compiled bytecode to enable "magic refactor" functionality--renaming methods, tracing up and down class hierarchies and tracing through method calls.
What technical barriers exist that make this harder to do for languages like Python and Javascript?
回答1:
Because of dynamic binding. Python is a dynamic language in a way that you can do almost everything with your variables. You can even access the globals-dict and introduce new variables composed of runtime values.
So an IDE can’t be sure which variables exist when. See this example:
#silly.py
import sys
if len(sys.argv) > 1:
thisNowExists = True
#1
try:
if thisNowExists:
print("this existed before")
except NameError:
print("this _now_ exists")
thisNowExists = True
No human or IDE can know if thisNowExists
is defined at the position #1
, so if you want to rename the stupidly named thisNowExists
below that point, it is undefined if we should rename the appearance before #1
, too.
You would have to do advanced control flow analysis to take a good guess that thisNowExists
is defined below the try/catch statement, but due to dynamic loading of the script (thisNowExists = 1; import silly
) and sorts, it could even exist before import sys
without arguments.
naming your variables differently and find/replace is your best option ;)
回答2:
so it turns out that tracing of static information like methods and class hierarchies is perfectly possible in python. PyDev eclipse plugin does it. PyLint plugin attempts to do static analysis even on stuff like dynamic variables by assuming that nothing funky happens at runtime and does a pretty good job.
来源:https://stackoverflow.com/questions/4995842/why-doesnt-eclipse-python-have-magic-refactor