class instance scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ritesh Raj Sarraf

    #1

    class instance scope

    Hi,

    I have a class defined in a file called foo.py

    In bar.py I've imported foo.py
    In bar.py's main function, I instantiate the class as follows:

    log = foo.log(x, y, z)

    Now in main I'm able to use log.view(), log.error() et cetera.

    But when I call the same method from some functions which are in
    bar.py, it fails giving me the following error:

    NameError: global name 'log' is not defined

    1) I tried lookng into the docs but couldn't find anything on instance
    scope.
    2) How is such situation tackled ? Will I have to instantiate in every
    function ?

    Ritesh

  • Steve Holden

    #2
    Re: class instance scope

    Ritesh Raj Sarraf wrote:
    Hi,
    >
    I have a class defined in a file called foo.py
    >
    In bar.py I've imported foo.py
    In bar.py's main function, I instantiate the class as follows:
    >
    log = foo.log(x, y, z)
    >
    Now in main I'm able to use log.view(), log.error() et cetera.
    >
    Correct. Because, having instantiated the class and retained a reference
    to the instance, the methods of the instance are available relative to
    the name containing the reference.
    But when I call the same method from some functions which are in
    bar.py, it fails giving me the following error:
    >
    NameError: global name 'log' is not defined
    >
    Well, that's preumbaly because your

    log = foo.log(x, y, z)

    statement was inside a function, and so the name "foo" was created in
    that function's local namespace rather than in the module's global
    namespace.
    1) I tried lookng into the docs but couldn't find anything on instance
    scope.
    2) How is such situation tackled ? Will I have to instantiate in every
    function ?
    >
    The best thing to do would be to pass the instance in as an argument to
    the functions that need to manipulate it.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden

    Comment

    • Ritesh Raj Sarraf

      #3
      Re: class instance scope


      Steve Holden wrote:
      Ritesh Raj Sarraf wrote:
      But when I call the same method from some functions which are in
      bar.py, it fails giving me the following error:

      NameError: global name 'log' is not defined
      Well, that's preumbaly because your
      >
      log = foo.log(x, y, z)
      >
      statement was inside a function, and so the name "foo" was created in
      that function's local namespace rather than in the module's global
      namespace.
      >
      So if I do the instantiation before calling main(), will it work.
      Something like:

      if __name__ == "__main__":
      log = foo.log(x, y, z)
      main()

      In this case, will log be global ?
      But still I get the same error.
      1) I tried lookng into the docs but couldn't find anything on instance
      scope.
      2) How is such situation tackled ? Will I have to instantiate in every
      function ?
      The best thing to do would be to pass the instance in as an argument to
      the functions that need to manipulate it.
      >
      But then how do os, sys, and other modules which are imported, become
      accessible to all the functions ?

      I'm a newbie, so please bear with me.

      Ritesh

      Comment

      • Ritesh Raj Sarraf

        #4
        Re: class instance scope

        log = foo.log(x, y, z)
        Resulting line is:
        log = foo.log(x, y, z)
        global log

        Making the instance "log" global makes it accessible to all the
        functions.

        Now I have only one question, Is this a correct way to do it ? Or are
        there better way ?

        Ritesh

        Comment

        • Jeethu Rao

          #5
          Re: class instance scope

          You could possibly make the log class a singleton or a borg.

          Jeethu Rao

          Ritesh Raj Sarraf wrote:
          >>log = foo.log(x, y, z)
          >>>
          >>>
          >
          Resulting line is:
          log = foo.log(x, y, z)
          global log
          >
          Making the instance "log" global makes it accessible to all the
          functions.
          >
          Now I have only one question, Is this a correct way to do it ? Or are
          there better way ?
          >
          Ritesh
          >
          >

          Comment

          Working...