1

I have the following pattern:

databasename = "mydatabase"
import databasefunctions

and in databasefunctions I have the following method.

def checkStatusOfDatabase(database=databasename):

I however get an error saying: NameError: name 'databasename' is not defined on the line defining checkStatusOfDatabase.

I could of course just move the definition of databasename inside the databasefunctions module, but that would not be suitable in my design. How else can I achieve that the databasename defined in the main module is utilized as the default in any imported modules.

David
  • 4,786
  • 11
  • 52
  • 80
  • Out of curiosity, why are you unable to do the definition in the databasefunctions module? – aodj Jan 21 '11 at 11:25
  • Not an exact duplicate, but this should answer your question to some extent: http://stackoverflow.com/questions/67631/how-to-import-module-from-file-name – viraptor Jan 21 '11 at 11:28
  • I would like to have all the parameter values right in the beginning of my main module (because I modify them a lot) and not hidden away in different imported modules. – David Jan 21 '11 at 11:29
  • @viraptor. That answer is related but does not help me as I am already definining the variable before I import, but that does not help. – David Jan 21 '11 at 11:31
  • sorry, I misunderstood your question - I read it as if you were trying to load a module called "mydatabase"... You're right it does not answer the question. – viraptor Jan 21 '11 at 11:39

2 Answers2

4

You cannot (and should not) do that; doing so would make it impossible to just import databasefunctions without the main module. However, you can implement a default like this:

# databasefunctions
DEFAULT_NAME = ""
def checkStatusOfDatabase(database=DEFAULT_NAME):

# main
import databasefunctions
databasefunctions.DEFAULT_NAME = "mydatabase"
phihag
  • 278,196
  • 72
  • 453
  • 469
  • What an excellent answer! I can accept it in 4 minutes (Stackoverflow obviously does not believe in quick correct answers!?). – David Jan 21 '11 at 11:33
  • Could I write the following in main instead: from databasefunctions import * and then on the following line just: DEFAULT_NAME = "mydatabase" – David Jan 21 '11 at 11:42
  • phihap: Unfortunately, you *can* do that, by writing `from __main__ import databasename` in the databasefunctions module. But I absolutely agree that you *should* not, and your alternative is the Right Way. – slowdog Jan 21 '11 at 12:29
  • David: That would not work because the assignment `DEFAULT_NAME = "mydatabase"` only makes the name `DEFAULT_NAME` refer to something different within your main module. The assignment `databasefunctions.DEFAULT_NAME = "mydatabase"`, on the other hand, changes the meaning of `DEFAULT_NAME` within the databasefunctions module. It's a subtle distinction, and surprising if you're used to other languages, like C, where assignments modify objects. – slowdog Jan 21 '11 at 12:41
0

You can use the __builtin__ module

import __builtin__
__builtin__.foo = 'bar'

This will then be accessible from all other modules when you do a similar access method. You could use this and do this:

import __builtin__
dbName = __builtin__.get('foo', 'ham') # dictionary access with default return
def checkStatusOfDatabase(database=dbName):
    ...
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
aodj
  • 2,163
  • 1
  • 12
  • 13