Testing if a global is defined in a module

Tim Johnson tim at johnsons-web.com
Mon Jul 4 22:30:39 CEST 2011


* rantingrick <rantingrick at gmail.com> [110704 12:00]:
> On Jul 4, 1:11 pm, Tim Johnson <t... at johnsons-web.com> wrote:
> 
> Well if you follow the python style guide (and most accepted styles
> for global notation) then it's a trial exercise. You don't even have
> to import anything!!! :)
> 
> >>> GLOBAL_STR = 'str'
> >>> GLOBAL_FLOAT = 1.33333
> >>> GLoBaL_Bs = ''
> >>> dir()
> ['GLOBAL_FLOAT', 'GLOBAL_STR', 'GLoBaL_Bs', '__builtins__', '__doc__',
> '__name__', '__package__', 'item']
> >>> for item in dir():
> 	if item.isupper():
> 		print 'Found Global!', item
  Thanks for the reply: *but*

  dir(<targetmodule>) will also show globals from other modules imported
  by the target module. So I would need a way to distinguish between
  those imported and those defined in <targetmodule>
  print(dir(targetmodule)) => 
   ['Install', 'TestAddresses', '__builtins__', '__doc__',
   '__file__', '__name__', '__package__', 'chmod', 'consoleMessage',
   'cp', 'debug', 'erh', 'exists', 'halt', 'is_list', 'load',
   'makePath', 'mkdir', 'process', 'sys', 'traceback', 'usingCgi'] 
   where 'TestAddresses' is a member of an imported module and
   'usingCgi' is the only data variable defined in <targetmodule>

  regards
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com



More information about the Python-list mailing list