Best practice: Sharing object between different objects

Chris Angelico rosuav at gmail.com
Mon Feb 23 19:14:57 EST 2015


On Tue, Feb 24, 2015 at 8:10 AM, Michael Torrie <torriem at gmail.com> wrote:
> On 02/23/2015 01:02 PM, sohcahtoa82 at gmail.com wrote:
>> That behavior always trips me up.  My intuition tells me that every
>> time you import a module, it re-runs the code in the module.  So if I
>> had a simple module named myModule.py that had a single line with
>> `myInt = 1`, then I would *EXPECT* this behavior:
>
> I can see how coming from PHP would trip you up, though.  Of course I
> never got the hang of include vs include_once vs require vs require_once.

If you think of "import" as "go and grab this file of code and run it"
(like PHP's include or a preprocessor #include directive), then yes,
you would expect that. But think of it, instead, as "give me access to
this feature". If that feature doesn't yet exist, Python will go and
fetch it up; but if it does, yay! You now have access, really quickly
and easily.

ChrisA



More information about the Python-list mailing list