When do we need Python Import Statements?

python, python-import

Solution

Python is a dynamically typed language. Unlike statically typed languages like C++ and Java calls to methods aren't bound until they are actually executed, thus why importing the module were that method is defined is not necessary. This has several implications:

- Methods (and data members) can be added to and removed from an instance at runtime, so two instances of class Foo can actually have different methods even though they are of the same type.

- Methods (and data members) can be added to and removed from a class at runtime, which will impact all current instances as well as new instances.

- Bases classes can be added and removed to a class at runtime.

Note that this is not an exhaustive list of all of the difference between dynamically typed langauges and statically types languages.

Problem

A piece of code works that I don't see why. It shouldn't work from my understanding. The problem is illustrated easily below: "Main.py" ``` from x import * #class x is defined from y import * #class y is defined xTypeObj = x() yTypeObj = y() yTypeObj.func(xTypeObj) ``` "x.py" ``` class x(object): def __init__... ... def functionThatReturnsAString(self): return "blah" ``` "y.py" ``` #NO IMPORT STATEMENT NEEDED?? WHY class y(object): def __init__... ... def func(self, objOfTypeX): print(objOfTypeX.functionThatReturnsAString()) ``` My question is why do I NOT need to have an import statement in "y.py" of the type ``` from x import functionThatReturnAString() ``` How does it figure out how to call this method?

Original source