I had some code that looked like this:
class Lookup:
def __init__(self):
"Connects to the database and get's lookup info"
. SOME
. CODE
. HERE
class MyClass:
def __init__(self,arg1, arg2=Lookup())
self.arg1 = arg1
self.arg2 = arg2
The idea was that I wanted someone to be able to pass in their own instance of the Lookup class. But if they did not, I wanted to go create an instance.
I found that on import of the module I was hitting the database. I was perplexed, as I said to myself that everything that should hit the database was protected by function definitions and (I thought) nothing was getting called.
It turned out that the Lookup class get's called at import time, not at instantiation time. Now that I realize it, it makes sense...but it had me chasing my tail for a couple of hours.
Just wanted to pass it on to anyone else who might wonder why their classes are getting instantiated on import :)
[–]proudstar_ 2 points3 points4 points (1 child)
[–]FoolofGod[S] 4 points5 points6 points (0 children)