0

What I'm trying to do may be easier to explain by example. Consider the following code:

In file my_custom_module.py

def oop_type_a():
    __init__(self):
    # class definitions, etc...

def oop_type_b():
    __init__(self):
    # class definitions, etc...

def define_a_bunch_of_oop_variables():
    global oop1
    global oop2

    oop1 = oop_type_a()
    oop2 = oop_type_b()

Using this file, I can run these commands:

>>>import my_custom_module
>>>my_custom_module.define_a_bunch_of_oop_variables()
>>>from my_custom_module import *

>>>oop1
<my_custom_module.oop_type_a object at 0x000000000615F470>
>>>oop2
<my_custom_module.oop_type_a object at 0x000000000615F5C0>

Is there a way to get this same functionality without the from my_custom_module import *? What is the pythonic way to achieve this?

I have several objects that I need to initialize everytime I use this module, and doing this is tedious:

>>>import my_custom_module
>>>oop1 = my_custom_module.oop_type_a()
>>>oop2 = my_custom_module.oop_type_b()
Jonathan Wheeler
  • 2,539
  • 1
  • 19
  • 29
  • It is not Pythonic to have a module create names outside its own namespace. – BrenBarn Dec 25 '16 at 18:09
  • That's fair. Is the Pythonic way to achieve this to simply have a text file of `oop1 = my_custom_module.oop_type_a()` commands that I copy-paste every time I use this module? – Jonathan Wheeler Dec 25 '16 at 18:11
  • It's actually not a general programming way to define such global variables and use it outside of your module. The root cause of your problem is how you organise the code and logic. – Simon J. Liu Dec 25 '16 at 18:19
  • If this is just a convenience for defining various variables (say for interactive use), then it seems like doing the `from foo import *` is fine. That would be easier than copying and pasting a bunch of code. But in general needing to define a bunch of specific variables is itself often a sign of dubious design. – BrenBarn Dec 25 '16 at 18:19
  • The use case is for research. I need to initialize GPIB connections to a bunch of instruments. `lia = my_custom_module.lock_in_amplifier()`, `daq = my_custom_module.data_acquisition_unit()` etc... Everytime I start an experiment, I need to load in all of these instrument wrappers. Then in my code I use `lia.auto_phase()` `daq.acquire(seconds=100)` – Jonathan Wheeler Dec 25 '16 at 18:21
  • It sounds like your real question is somewhat different than what you've asked here. Why can't you write a function that calls `lia.auto_phase()`, etc., and then call that one function instead of having to rewrite `lia.auto_phase()` etc. over and over? – BrenBarn Dec 25 '16 at 18:24
  • For one thing, you seem to have confused `def` and `class`... – jonrsharpe Dec 25 '16 at 18:25
  • Because `lia` wraps a bunch of functions and properties, `freq` `phase` `sensitivity` `integration_time` `auto_phase()` `auto_gain()` `identify()`, not to mention that I might use several different lock-in amplifiers in my lab, and I want the class to be abstract so I can put in an Agilent or Stanford Research Systems instrument. I figured when somebody looks at the code I used in my experiments 5 years from now, they can see 'ok, he set up the `lia`, then autophased and autogained it, then recorded the frequency and phase, and then acquired 100 seconds of data with the `daq`, then....' – Jonathan Wheeler Dec 25 '16 at 18:26
  • None of that sounds incompatible with wrapping the entire initialization process into a function that returns (or creates) whatever you need to use directly. I think it would be helpful if you edit your question to give more context about what you really need to do. If nothing else, you can do `import my_custom_module as mcm` so you can type `mcm.oop1` instead of the longer `my_custom_module.oop1`. – BrenBarn Dec 25 '16 at 18:28
  • Maybe. My understanding is that SO doesn't want questions on specific use cases, but rather Q&A's that can be useful to a broad range of applications. – Jonathan Wheeler Dec 25 '16 at 18:30
  • The actual name of the module that I've defined is `pyfog` which I guess I could run `import pyfog as pf` but even so, I'd still be running commands as `pf.lia.autophase()` `pf.awg.frequency = 98e6 #Hz` which seems less elegant than the same commands without the module in front. – Jonathan Wheeler Dec 25 '16 at 18:31

1 Answers1

0

The module is used to separate variables and methods. If you insist maybe you can try make a package and declare the global variables in __init__.py

refer this: Can I use __init__.py to define global variables?

Community
  • 1
  • 1
Simon J. Liu
  • 787
  • 4
  • 11