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()
Based on the background of C ++, I got the feeling that you need to import a module containing the definition of the Universe class before the show_answer function works. That is, everything had to be announced before it could be used.
Am I right in thinking that this is not necessary? This is a duck, right? So if import is not required to view class methods, would I at least need it to define the class and top-level functions of the module itself?
In one script that I wrote, I even went as far as writing the base class to declare an interface with a set of methods, and then output specific classes to inherit from that interface, but I think I'm getting it now - which is wrong in Python, and is the object checked by a certain method at runtime at the point where the call is made?
I understand that Python is much more dynamic than C ++, it took me a while to see how little code you really need to write!
I think I know the answer to this question, but I just wanted to get clarification and make sure I'm on the right track.
UPDATE: Thanks for all the answers, I think I should clarify my question now:
Does mod2.show_answer () need to import (of any description) to know that this thing has a method called answer (), or is it determined dynamically at runtime?
source share