Global utility module/package

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christoph Haas

    #1

    Global utility module/package

    Evening,

    I'm currently working on a larger Python project that consists of multiple
    programs and packages. As I need a few utility functions time and again I
    moved them all into a Utility package and created a class there. Like this:

    Util.py:
    ~~~~~~~~
    class Util:
    def __init__(self, debugFlag=False , vibranceLevel=' good'):
    self.debugFlag = debugFlag
    self.vibranceLe vel = vibranceLevel

    def function1(self) :
    do this
    do that

    main.py:
    ~~~~~~~~
    import Util
    util = Util.Util()
    util.function1( whatever)

    def some_function() :
    global util
    util.function1( dingdong)

    However this feels like I'm abusing classes because I don't really need
    several instances of an object. I just need one instance. Even worse is
    that I'm starting to use "global util" to get access to the "alibi
    instance" I created. (I'm not sure whether I could even omit it due to
    scoping rules.)

    As I know that importing packages from multiple modules always keeps it a
    singleton I thought of something like this:

    Util.py:
    ~~~~~~~~
    debugFlag = False
    vibranceLevel = 'good'

    def function1():
    global debugFlag
    print debugFlag

    main.py:
    ~~~~~~~~
    import Util
    Util.debugFlag = True
    Util.function1( whatever)

    def doThis():
    Util.function1( 42)

    Here I don't use classes any longer. Good. But to access the "package
    variables" I probably need to use "global" again which just moved the
    ugliness to another position.

    What would be a good practice here? All I want is a utility package that I
    can import from everywhere (as a singleton) and that I can use even in
    subroutines (def) without needing to write "global" here. (The "global"
    could probably even be omitted because unless I define a variable in a
    "def" scope the global variable should be visible.)

    Please enlighten me. :)

    Kindly
    Christoph

    P.S.: Code parts untested. More a schema than something that would actually run.
    --
    Please reply to the list - not to me personally.
  • Scott David Daniels

    #2
    Re: Global utility module/package

    Christoph Haas wrote:[color=blue]
    > As I know that importing packages from multiple modules always keeps it a
    > singleton I thought of something like this:
    >
    > Util.py:
    > ~~~~~~~~
    > debugFlag = False
    > vibranceLevel = 'good'
    >
    > def function1():
    > global debugFlag
    > print debugFlag[/color]

    The global line is not needed: global declarations are only required
    if you need to _write_ on globals w/in the function.

    --Scott David Daniels
    scott.daniels@a cm.org

    Comment

    • Kent Johnson

      #3
      Re: Global utility module/package

      Christoph Haas wrote:[color=blue]
      > Evening,
      >
      > I'm currently working on a larger Python project that consists of multiple
      > programs and packages. As I need a few utility functions time and again I
      > moved them all into a Utility package and created a class there.[/color]
      ....[color=blue]
      > As I know that importing packages from multiple modules always keeps it a
      > singleton I thought of something like this:
      >
      > Util.py:
      > ~~~~~~~~
      > debugFlag = False
      > vibranceLevel = 'good'
      >
      > def function1():
      > global debugFlag
      > print debugFlag
      >
      > main.py:
      > ~~~~~~~~
      > import Util
      > Util.debugFlag = True
      > Util.function1( whatever)
      >
      > def doThis():
      > Util.function1( 42)
      >
      > Here I don't use classes any longer. Good. But to access the "package
      > variables" I probably need to use "global" again which just moved the
      > ugliness to another position.[/color]

      This is fine. You don't need 'global' statements to read global
      variables, function1() can be simply
      def function1():
      print debugFlag

      Kent

      Comment

      Working...