Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - when is 'import' required?

mod1.py

import mod2

class Universe:
    def __init__(self):
        pass
    def answer(self):
        return 42

u = Universe()
mod2.show_answer(u)

mod2.py

#import mod1 -- not necessary
def show_answer(thing):
    print thing.answer()

Coming from a C++ background I had the feeling it was necessary to import the module containing the Universe class definition before the show_answer function would work. I.e. everything had to be declared before it could be used.

Am I right in thinking this isn't necessary? This is duck typing, right? So if an import isn't required to see the methods of a class, I'd at least need it for the class definition itself and the top level functions of a module?

In one script I've written, I even went as far as writing a base class to declare an interface with a set of methods, and then deriving concrete classes to inherit that interface, but I think I get it now - that's just wrong in Python, and whether an object has a particular method is checked at runtime at the point where the call is made?

I realise Python is so much more dynamic than C++, it's taken me a while to see how little code you actually need to write!

I think I know the answer to this question, but I just wanted to get clarification and make sure I was on the right track.

UPDATE: Thanks for all the answers, I think I should clarify my question now:

Does mod2.show_answer() need an import (of any description) to know that thing has a method called answer(), or is that determined dynamically at runtime?

like image 446
Steve Folly Avatar asked Jun 22 '09 14:06

Steve Folly


People also ask

Do you need to import in Python?

Importing modules is an important part of working with Python that allows you to call functions that are not part of your main program. The modules you work with can be part of Python, installed using pip, or created by you.

Do I need to import in every Python file?

Python does not actually import a module that it has already imported (unless you force it to do so with the reload function), so you can safely put a import some_module statement into every module of your program that needs access to the names defined in some_module .

Should you put Imports in functions Python?

PEP 8 recommends putting imports at the top. It's often more convenient to get ImportError s when you first run your program rather than when your program first calls your function. Putting imports in the function scope can help avoid issues with circular imports.

Why is the use of import all statement not recommended in Python?

Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import.


1 Answers

In this case you're right: show_answer() is given an object, of which it calls the method "answer". As long as the object given to show_answer() has such a method, it doesn't matter where the object comes from.

If, however, you wanted to create an instance of Universe inside mod2, you'd have to import mod1, because Universe is not in the mod2 namespace, even after mod2 has been imported by mod1.

like image 92
balpha Avatar answered Nov 09 '22 06:11

balpha