Hello

I frequent the PyGtk mailing list all the time, but this is the first time I am 
posting here =)

I wanted to know if imported classes are treated differently than internal 
classes.

I have attached a minimal example which points out what I mean. Essentially, if 
I create a class:

class A():
 __init__(self):
  pass

and call A, or A() I get something to the effect of  <__main__.A instance at 
0xb7f088ac>

but if I call from module_name import A and then call A or A() I get something 
to the effect of  <module_4.module_4 instance at 0xb7f0882c>

I was wondering if this would be a problem.

I have included the test_import.py file as well as 4 module files which it 
loads. Save the tes_import.py file to a folder, create another folder there, 
and save ONLY the 4 modules files to that new folder. In the code, update line 
26 to reflect to path of the newly created directory

Thanks in advanced


Peyman

#!/usr/bin/env python

import sys
import os

#This is a dummy class to determine how internal classes behave differenlt from those loaded out of a module
class A:
	def __init__(self):
		pass

def load_modules():
	"""Goes through the directory specified, and attempts to load in all the modules

	   NOTE: Change 'directory' to point to the location of your module_x.py files
	"""
	global simulatorModelPath
	global neuronModelPath
	global synapseModelPath
	global neuronFunctionPath
	global plasticityModelPath
	global connectionFunctionPath
	global environmentTypePath
	global navigationFunctionPath

	#folder containing modules modules_x.py with each module containg the class module_x
	directory='/home/peyman/simulator/sandbox/modules/'

	#add the folder to the path search directory
	sys.path.append(directory)

	#list containing names of all modules to load
	moduleNames=[]

	#list containing calls to __init__ (used for debugging later)
	initializers=[]

	#iterate over every file
	for fileName in os.listdir(directory):
		#only consider python scripts
		if fileName[len(fileName)-3:]=='.py':
			#strip trailing '.py'
			fileName=fileName[:len(fileName)-3]
			
			#add to module list
			moduleNames.append(fileName)

	#iterate over every module name and import it
	for moduleName in moduleNames:
		#import the module
		try:
			command='from %s import %s'%(moduleName,moduleName)
			exec command
		except:
			print 'could not import %s'%moduleName

			#append the initializer to the initializers list
		try:
			command='initializers.append(%s)'%moduleName
			exec command
		except:
			print 'could not append %s'%moduleName

	#lets see what was written to the initializers
	print 'Initializers are represented as module_x.module_x, while the internal class is represented as __main__.A:'
	for initializer in initializers:
		print '\t',initializer,' v ', A

	#lets see if we can create objects out of those initializers and contrast it with class A
	print '\nHere again objects are represented as module_x.module_x, while the internal class is represented as __main__.A:'
	for initializer in initializers:
		obj=initializer()
		objA=A()
		print '\t',obj,' v ',objA


if __name__ == '__main__':
	load_modules()



#!/usr/bin/env python

class module_1:
	def __init__(self):
		pass
#!/usr/bin/env python

class module_2:
	def __init__(self):
		pass
#!/usr/bin/env python

class module_3:
	def __init__(self):
		pass
#!/usr/bin/env python

class module_4:
	def __init__(self):
		pass
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to