private variables/methods

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

    private variables/methods

    hi,

    as far as i know in python there aren't any private (i mean not accessible
    from the outside of the object) methods/fields.

    why?

    in java/c++ i can make a method private, this way unaccessible for the outside
    world. i think it helps a lot to make for example a library more robust.

    i know that there is some kind of notation to make a method/field private,
    but one can still overwrite it's value.

    what's the reason for this?

    i'l mostly interested in the design reasons.

    thanks,
    gabor

    --
    That's life for you, said McDunn. Someone always waiting for someone
    who never comes home. Always someone loving something more than that
    thing loves them. And after awhile you want to destroy whatever
    that thing is, so it can't hurt you no more.
    -- R. Bradbury, "The Fog Horn"

  • Dave Benjamin

    #2
    Re: private variables/methods

    In article <mailman.106568 5337.1770.pytho n-list@python.org >, gabor wrote:[color=blue]
    > as far as i know in python there aren't any private (i mean not accessible
    > from the outside of the object) methods/fields.
    >
    > why?[/color]

    No offense, but this is like the 4000th time someone has asked that question
    here. Could you try searching Google groups first?

    Dave

    --
    ..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
    : d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :

    Comment

    • gabor

      #3
      Re: private variables/methods

      On Thu, 2003-10-09 at 10:12, Dave Benjamin wrote:[color=blue]
      > In article <mailman.106568 5337.1770.pytho n-list@python.org >, gabor wrote:[color=green]
      > > as far as i know in python there aren't any private (i mean not accessible
      > > from the outside of the object) methods/fields.
      > >
      > > why?[/color]
      >
      > No offense, but this is like the 4000th time someone has asked that question
      > here. Could you try searching Google groups first?[/color]

      you're right, i'm sorry.

      after reading the archives:

      i think i didn't express myself too well.
      i'll try again:

      i'm aware of the fact that you can declare a variable private with "__".
      but i just don't like prefix-notation. i don't like the mMember or
      lpszCommandLine style notation, and also the $variable style ones.

      at least for me it seemed that adding a "private" keyword somewhere is
      more elegant. but there isn't anything like that.

      that's why it seems to me that the designers of the language don't
      recommend you to use private variables at all, but if you want, you can
      use this 'hack' ( i mean the "__" notation).


      thanks,
      gabor


      Comment

      • Peter Hansen

        #4
        Re: private variables/methods

        gabor wrote:[color=blue]
        >
        > On Thu, 2003-10-09 at 10:12, Dave Benjamin wrote:[color=green]
        > > In article <mailman.106568 5337.1770.pytho n-list@python.org >, gabor wrote:[color=darkred]
        > > > as far as i know in python there aren't any private (i mean not accessible
        > > > from the outside of the object) methods/fields.
        > > >
        > > > why?[/color]
        > >
        > > No offense, but this is like the 4000th time someone has asked that question
        > > here. Could you try searching Google groups first?[/color]
        >
        > you're right, i'm sorry.
        >
        > after reading the archives:
        >
        > i think i didn't express myself too well.
        > i'll try again:[/color]

        Don't bother. You perhaps didn't really read enough of the archives,
        because Dave's point still stands. None of your questions or comments,
        as far as I can tell, ask or say anything that hasn't already been
        asked (and answered) or said before.

        In summary: it's more of a philosophical difference than anything, and
        Python simply doesn't *want* a "private" keyword, nor things like that
        which artificially restrict the programmer. (Again, even that comment
        adds nothing new, so you're really wasting your and our time by responding
        rather than continuing to read the zillion old threads on the topic.)

        -Peter

        Comment

        • Harri Pesonen

          #5
          Re: private variables/methods

          Peter Hansen wrote:[color=blue]
          > gabor wrote:
          >[color=green]
          >>On Thu, 2003-10-09 at 10:12, Dave Benjamin wrote:
          >>[color=darkred]
          >>>In article <mailman.106568 5337.1770.pytho n-list@python.org >, gabor wrote:
          >>>
          >>>>as far as i know in python there aren't any private (i mean not accessible
          >>>>from the outside of the object) methods/fields.
          >>>>
          >>>>why?
          >>>
          >>>No offense, but this is like the 4000th time someone has asked that question
          >>>here. Could you try searching Google groups first?[/color]
          >>
          >>you're right, i'm sorry.
          >>
          >>after reading the archives:
          >>
          >>i think i didn't express myself too well.
          >>i'll try again:[/color]
          >
          > Don't bother. You perhaps didn't really read enough of the archives,
          > because Dave's point still stands. None of your questions or comments,
          > as far as I can tell, ask or say anything that hasn't already been
          > asked (and answered) or said before.[/color]

          Because it has been asked 4000 times probably means that there is a
          great need for the feature...
          [color=blue]
          > In summary: it's more of a philosophical difference than anything, and
          > Python simply doesn't *want* a "private" keyword, nor things like that
          > which artificially restrict the programmer. (Again, even that comment
          > adds nothing new, so you're really wasting your and our time by responding
          > rather than continuing to read the zillion old threads on the topic.)[/color]

          "Python" doesn't want a "private" keyword? I have quite a limited Python
          experience but I would like to have the following features in Python
          that are common in other languages:

          * Option Explicit
          * variable type declaration (optional)
          * private variables/methods

          Most of these are handy for large projects, where you want to be sure
          that a class is not misused (by other developers). These also mean that
          it is much harder to create bugs. I like Python a lot, but with these
          features it would be much better for serious development of complex
          applications, not just for scripting.

          One thing I have noticed that the keyword "global" is very confusing.
          For example, the following is syntactically valid Python:

          a = 1
          def b():
          a = 2
          def c():
          return a

          But it does not work as expected. Function b just creates a new local
          variable "a" inside function b! The correct function is of course:

          def b():
          global a
          a = 2

          On the other hand, function c refers to the same global variable just
          fine without any extra "global" keyword. Why on earth?? :-) In every
          other language I know you don't need "global". It is ugly.

          Harri

          Comment

          • Alex Martelli

            #6
            Re: private variables/methods

            Harri Pesonen wrote:
            ...[color=blue]
            > Because it has been asked 4000 times probably means that there is a
            > great need for the feature...[/color]

            I can't think of ANY "feechur" from other popular languages that hasn't
            been asked for, thousands of times. Does this mean that "there is a
            great need" for each and all of them? Not at all: it just means that people
            hanker for what they're familiar with. If Python were to satisfy even
            1/10th of this incessant barrage of requests, it would devolve to a
            large amorphous blob -- like many other languages have. The people
            requesting these features are typically NOT experienced with Python:
            they haven't experienced how the LACK of these features in fact makes
            it easier and more productive to write application programs.

            [color=blue]
            > "Python" doesn't want a "private" keyword?[/color]

            If Python can be said to have a will -- embodied in Guido or spread
            as community consensus -- it definitely doesn't.
            [color=blue]
            > I have quite a limited Python
            > experience but I would like to have the following features in Python
            > that are common in other languages:[/color]

            Not 'but', but rather, THEREFORE. Reread your words with this
            change and with some luck you may get it.

            [color=blue]
            > * Option Explicit
            > * variable type declaration (optional)
            > * private variables/methods
            >
            > Most of these are handy for large projects, where you want to be sure
            > that a class is not misused (by other developers). These also mean that
            > it is much harder to create bugs. I like Python a lot, but with these
            > features it would be much better for serious development of complex
            > applications, not just for scripting.[/color]

            You are wrong. I used to harbor similar illusions (to a lesser degree,
            because I _did_ have SOME experience with other dynamic languages,
            but not in using them for really large apps) back when the language I
            most used was C++. I was wrong, too.

            Other developers aren't any likelier to "misuse" your class than you
            are to misdesign it in the first place -- and you'll NEVER "be sure"
            anyway, as restrictions can be worked around. _ADVISORY_
            indications of "privacy" -- the convention of starting the name with
            a single underscore -- are much simpler and equally effective for
            your purposes. Python is wonderfully effective for programming
            large applications, exactly as it is today.

            [color=blue]
            > One thing I have noticed that the keyword "global" is very confusing.[/color]

            You are right. It would be better if the current module could be
            imported -- by using some reserved special module name in a
            perfectly ordinary 'import' instruction -- so that global variables
            could then be re-bound as attributes of this module.

            Just to give you an idea, in today's Python you could add this
            feature as:

            # part to be executed once, e.g. in site.py
            import __builtin__, sys
            _base_import = __builtin__.__i mport__
            def __import__(name , *args):
            if name == '__current_modu le__':
            name = sys._getframe(1 ).f_globals['__name__']
            return _base_import(na me, *args)
            __builtin__.__i mport__ = __import__
            # end of part to be executed once

            # example use
            x = 23

            def set_the_global( ):
            import __current_modul e__
            __current_modul e__.x = 45

            print x
            set_the_global( )
            print x


            emits
            23
            45
            [color=blue]
            > For example, the following is syntactically valid Python:
            >
            > a = 1
            > def b():
            > a = 2
            > def c():
            > return a
            >
            > But it does not work as expected. Function b just creates a new local
            > variable "a" inside function b! The correct function is of course:
            >
            > def b():
            > global a
            > a = 2
            >
            > On the other hand, function c refers to the same global variable just
            > fine without any extra "global" keyword. Why on earth?? :-) In every[/color]

            Because reading is different from writing. Reading globals is (more or
            less) all right; writing globals is a delicate decision that is well worth
            emphasizing. So, anything that's written (any name that's re-bound,
            to be precise) is deemed to be local -- unless explicitly mentioned in
            a global statement.

            The problem with global is that it's not clear enough. If there simply
            was NO way at all to have any assignment to a bare name, such
            as "a=2", EVER affect anything BUT a local, things would be much
            clearer; the need to import __current_modul e__ would emphasize what
            a serious, think-twice-about-it decision it is to choose to rebind
            module-global names. It would also emphasize that 'global' means
            'of this module', not in any way of ALL modules -- a misconception
            that often affects newbies.

            Hmmm -- maybe THIS is worth proposing for 2.4 (with a
            pending deprecation warning for the global statement)...
            [color=blue]
            > other language I know you don't need "global". It is ugly.[/color]

            Problem is, it's not quite ugly enough (nor quite clear enough).
            Discouraging you from affecting globals is a swell idea, but I
            think the 'global' statement may not be enough for that, whence
            my newly conceived suggestion about importing...


            Alex

            Comment

            • Scott David Daniels

              #7
              Re: private variables/methods

              Alex Martelli wrote:[color=blue]
              > ...
              > I can't think of ANY "feechur" from other popular languages that hasn't
              > been asked for, thousands of times....[/color]
              Aha! caught the Martellibot in a rare memory failure (perhaps a rare
              double-bit parity error)? To my knowledge, nobody has suggested the
              autodeclaration of variables which begin with the letters 'I' through
              'N' as integer variables. So there. :-)

              Now watch, he'll document 1003 requests on alt.lang.python .it.

              -Scott David Daniels
              Scott.Daniels@A cm.Org

              Comment

              • Jordan Krushen

                #8
                Re: private variables/methods

                On Fri, 10 Oct 2003 22:22:04 GMT, Alex Martelli <aleaxit@yahoo. com> wrote:
                [color=blue]
                > The problem with global is that it's not clear enough. If there simply
                > was NO way at all to have any assignment to a bare name, such
                > as "a=2", EVER affect anything BUT a local, things would be much
                > clearer; the need to import __current_modul e__ would emphasize what
                > a serious, think-twice-about-it decision it is to choose to rebind
                > module-global names. It would also emphasize that 'global' means
                > 'of this module', not in any way of ALL modules -- a misconception
                > that often affects newbies.[/color]
                [color=blue]
                > Hmmm -- maybe THIS is worth proposing for 2.4 (with a
                > pending deprecation warning for the global statement)...[/color]

                I'm rather amused at the thought of a module importing itself. I find it
                cleaner than global, and it also emphasizes that modules are singletons,
                if you think about how a module *can* import itself.

                I think that __current_modul e__ is perhaps a bit too lengthy (although I
                appreciate the motivation behind this), but aside from that, I like it.

                I'm also pleased with the ouroboros-like imagery it conjures..

                J.

                Comment

                • Dennis Lee Bieber

                  #9
                  Re: private variables/methods

                  Harri Pesonen fed this fish to the penguins on Friday 10 October 2003
                  12:16 pm:

                  [color=blue]
                  >
                  > Because it has been asked 4000 times probably means that there is a
                  > great need for the feature...
                  >[/color]
                  Or it just means that there are 4000 undisciplined programmers out
                  there who can't trust their own coding and require the language to
                  protect them from themselves.

                  For them, I suggest coding in Ada...
                  [color=blue]
                  >
                  > "Python" doesn't want a "private" keyword? I have quite a limited
                  > Python experience but I would like to have the following features in
                  > Python that are common in other languages:
                  >
                  > * Option Explicit[/color]

                  Only found in M$ Basic dialects as I recall. Real BASIC only required
                  declarations for arrays (needed the size), and used special suffix
                  characters for type identification.
                  [color=blue]
                  > * variable type declaration (optional)[/color]

                  Well, if you posit an option explicit statement, being required to
                  declare variables would become an option...
                  [color=blue]
                  > * private variables/methods
                  >[/color]

                  Why? You don't trust yourself to stay away from "internal" details
                  when using a class?

                  Let's see... Languages that don't require variable declarations:

                  BASIC (though it does allow suffix character to differentiate number
                  from string)
                  FORTRAN (though it uses variables beginning I-N as integers, all others
                  are real)
                  Python (no restriction, any variable can refer to any type of object)
                  REXX (no restriction that I know of)
                  DCL (and most command line scripting languages)
                  APL
                  LISP (at least, the versions up to the early 80s)
                  FORTH

                  Languages that require declarations for all variables:

                  Ada
                  COBOL
                  C++
                  Java
                  assembly (well, you have to declare the storage space at least)



                  --[color=blue]
                  > =============== =============== =============== =============== == <
                  > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                  > wulfraed@dm.net | Bestiaria Support Staff <
                  > =============== =============== =============== =============== == <
                  > Bestiaria Home Page: http://www.beastie.dm.net/ <
                  > Home Page: http://www.dm.net/~wulfraed/ <[/color]

                  Comment

                  • Dennis Lee Bieber

                    #10
                    Re: private variables/methods

                    Scott David Daniels fed this fish to the penguins on Friday 10 October
                    2003 17:09 pm:
                    [color=blue]
                    >
                    > Now watch, he'll document 1003 requests on alt.lang.python .it.
                    >[/color]
                    No... What he'll find are requests that variables beginning A-H and
                    O-Z be floats....

                    --[color=blue]
                    > =============== =============== =============== =============== == <
                    > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                    > wulfraed@dm.net | Bestiaria Support Staff <
                    > =============== =============== =============== =============== == <
                    > Bestiaria Home Page: http://www.beastie.dm.net/ <
                    > Home Page: http://www.dm.net/~wulfraed/ <[/color]

                    Comment

                    • Alex Martelli

                      #11
                      Re: private variables/methods

                      Scott David Daniels wrote:
                      [color=blue]
                      > Alex Martelli wrote:[color=green]
                      >> ...
                      >> I can't think of ANY "feechur" from other popular languages that hasn't
                      >> been asked for, thousands of times....[/color]
                      > Aha! caught the Martellibot in a rare memory failure (perhaps a rare
                      > double-bit parity error)? To my knowledge, nobody has suggested the
                      > autodeclaration of variables which begin with the letters 'I' through
                      > 'N' as integer variables. So there. :-)[/color]

                      Darn. I should have added a word -- a "current" or "currently" somewhere.
                      Arithmetic IF, variable types based on initial letter of the name, and
                      other such features are not in the _currently_ popular versions of Fortran
                      (and I'm sure we can find early-60, now-deplored feechurs of Cobol or
                      RPG that are also rarely or never asked for).

                      [color=blue]
                      > Now watch, he'll document 1003 requests on alt.lang.python .it.[/color]

                      that's it.comp.lang.py thon -- and I don't think I've met many old Fortran-IV
                      hands there...


                      Alex

                      Comment

                      • Nick Vargish

                        #12
                        Re: private variables/methods

                        Scott David Daniels <Scott.Daniels@ Acm.Org> writes:
                        [color=blue]
                        > Aha! caught the Martellibot in a rare memory failure (perhaps a rare
                        > double-bit parity error)? To my knowledge, nobody has suggested the
                        > autodeclaration of variables which begin with the letters 'I' through
                        > 'N' as integer variables. So there. :-)[/color]

                        I think two key phrases were "popular language" and "feature".

                        Nick

                        --
                        # sigmask || 0.2 || 20030107 || public domain || feed this to a python
                        print reduce(lambda x,y:x+chr(ord(y )-1),' Ojdl!Wbshjti!=o bwAcboefstobudi/psh?')

                        Comment

                        • Terry Reedy

                          #13
                          Re: private variables/methods


                          "Jordan Krushen" <jordan@krushen .com> wrote in message
                          news:oprwu4uvju 5ctagx@shawnews ...[color=blue]
                          > I'm rather amused at the thought of a module importing itself. I[/color]
                          find it[color=blue]
                          > cleaner than global, and it also emphasizes that modules are[/color]
                          singletons,[color=blue]
                          > if you think about how a module *can* import itself.[/color]

                          You can already do this in the main module:
                          [color=blue][color=green][color=darkred]
                          >>> __main__[/color][/color][/color]
                          Traceback (most recent call last):
                          File "<stdin>", line 1, in ?
                          NameError: name '__main__' is not defined[color=blue][color=green][color=darkred]
                          >>> __name__[/color][/color][/color]
                          '__main__'[color=blue][color=green][color=darkred]
                          >>> import __main__
                          >>> __main__[/color][/color][/color]
                          <module '__main__' (built-in)>[color=blue][color=green][color=darkred]
                          >>> a=3
                          >>> __main__.__dict __['a'][/color][/color][/color]
                          3[color=blue][color=green][color=darkred]
                          >>> __main__.__dict __['b'] = 5
                          >>> b[/color][/color][/color]
                          5
                          [color=blue][color=green][color=darkred]
                          >>> del __main__
                          >>> __main__[/color][/color][/color]
                          NameError: name '__main__' is not defined[color=blue][color=green][color=darkred]
                          >>> globals()[__name__] = __import__(__na me__)
                          >>> __main__[/color][/color][/color]
                          <module '__main__' (built-in)>

                          I believe globals line above will work in imported modules, in which
                          __name__ is import name (file name if from file), but have not tested
                          it in such.
                          [color=blue]
                          > I think that __current_modul e__ is perhaps a bit too lengthy[/color]

                          and redundant ;-)

                          Terry J. Reedy


                          Comment

                          • Alex Martelli

                            #14
                            Re: private variables/methods

                            Terry Reedy wrote:
                            ...[color=blue][color=green][color=darkred]
                            >>>> globals()[__name__] = __import__(__na me__)
                            >>>> __main__[/color][/color]
                            > <module '__main__' (built-in)>
                            >
                            > I believe globals line above will work in imported modules, in which
                            > __name__ is import name (file name if from file), but have not tested
                            > it in such.[/color]

                            Yes, this will work at the global level in any module. But it's not a
                            normal import instruction, while the modified builtin __import__ I
                            showed does allow normal importing; and your _use_ of __import__ and
                            globals() cannot bind a _local_ variable of a function to the current
                            module object.

                            [color=blue][color=green]
                            >> I think that __current_modul e__ is perhaps a bit too lengthy[/color]
                            >
                            > and redundant ;-)[/color]

                            I disagree. Lengthy it may be, but we do want a 'reserved module
                            name' to use for this purpose. For a normal import instruction to
                            work, and to work just as well if you cut and paste the same function
                            elsewhere, I think we want to define that "import somespecificnam e" is
                            importing THIS module, the CURRENT module, under that name (or another
                            specified with 'as'). This can be experimented with easily, by changing
                            the builtin __import__ (or setting an import hook, maybe) in site-specific
                            file; and if it catches on the builtin __import__ could easily be customized
                            to perform the same task quite speedily.


                            Alex

                            Comment

                            • Sean Ross

                              #15
                              Re: private variables/methods

                              "Alex Martelli" <aleaxit@yahoo. com> wrote in message
                              news:Iseib.2048 01$hE5.6891483@ news1.tin.it...[color=blue][color=green][color=darkred]
                              > >> I think that __current_modul e__ is perhaps a bit too lengthy[/color]
                              > >
                              > > and redundant ;-)[/color]
                              >
                              > I disagree. Lengthy it may be, but we do want a 'reserved module
                              > name' to use for this purpose. For a normal import instruction to
                              > work, and to work just as well if you cut and paste the same function
                              > elsewhere, I think we want to define that "import somespecificnam e" is
                              > importing THIS module, the CURRENT module, under that name (or another
                              > specified with 'as'). This can be experimented with easily, by changing
                              > the builtin __import__ (or setting an import hook, maybe) in site-specific
                              > file; and if it catches on the builtin __import__ could easily be[/color]
                              customized[color=blue]
                              > to perform the same task quite speedily.[/color]

                              Hi.
                              I'm not sure I'm clear on what behaviour "import __current_modul e__" is
                              expected to have.
                              There's a bit more to follow but I'll ask my main question up front so we're
                              clear:

                              "Which of the following behaviours is preferred?"

                              I've been playing with some code, using my own pet global workaround, and
                              I'm not sure
                              if it's accomplishing the correct behaviour. The results are certainly not
                              the same as yours.
                              in fact, I think the behaviour of my version may be unexpected and dangerous
                              ....

                              If inside module A you import a function f() from another module B that
                              tries to use the
                              current module directly, rather than global, to set variables and you call
                              that function
                              (i.e., B.f()), my version will affect the values of A's global variables
                              whereas yours does
                              not. For example, here is some output from a test run of each method. The
                              code for all
                              of this will be posted at the bottom of this message. Note: I've added some
                              print statements
                              to your code.


                              # Output of running setglobal1.py using import __current_modul e__
                              setglobal1.x = 23
                              ENTERING setglobal1.set_ the_global()
                              global x = 23
                              set __current_modul e__.x = 45
                              global x = 45
                              EXITING setglobal1.set_ the_global()
                              ENTERING setglobal2.set_ the_global()
                              global x = 57
                              set __current_modul e__.x = 88
                              global x = 88
                              EXITING setglobal2.set_ the_global()
                              setglobal1.x = 45


                              # Output of running main1.py using import __main__
                              main1.x = 23
                              ENTERING main1.foo()
                              global x = 23
                              set main.x = 45
                              global x = 45
                              EXITING main1.foo()
                              ENTERING main2.foo()
                              global x = 57
                              main2.x = 45
                              set main.x = 88
                              global x = 57
                              EXITING main2.foo()
                              main1.x = 88


                              As you can see, setglobal1.x = 45 in your version, but my analogous
                              variable main1.x = 88.

                              Here's the code:

                              #============== =============== =============== ==
                              # site.py
                              import __builtin__, sys
                              _base_import = __builtin__.__i mport__
                              def __import__(name , *args):
                              if name == '__current_modu le__':
                              name = sys._getframe(1 ).f_globals['__name__']
                              return _base_import(na me, *args)
                              __builtin__.__i mport__ = __import__
                              # end of part to be executed once

                              #============== =============== =============== ===
                              # setglobal1.py

                              import setglobal2
                              x = 23

                              def set_the_global( ):
                              print "ENTERING setglobal1.set_ the_global()"
                              import __current_modul e__
                              global x
                              print "global x = %s"%x
                              __current_modul e__.x = 45
                              print "set __current_modul e__.x = %s"%__current_m odule__.x
                              print "global x = %s"%x
                              print "EXITING setglobal1.set_ the_global()"

                              if __name__ == "__main__":
                              print "setglobal1 .x = %s"%x
                              set_the_global( )
                              setglobal2.set_ the_global()
                              print "setglobal1 .x = %s"%x


                              #============== =============== =============== ===
                              # setglobal2.py
                              x = 57

                              def set_the_global( ):
                              print "ENTERING setglobal2.set_ the_global()"
                              import __current_modul e__
                              global x
                              print "global x = %s"%x
                              __current_modul e__.x = 88
                              print "set __current_modul e__.x = %s"%__current_m odule__.x
                              print "global x = %s"%x
                              print "EXITING setglobal2.set_ the_global()"

                              if __name__ == "__main__":
                              print "setglobal2 .x = %s"%x
                              set_the_global( )
                              print "setglobal2 .x = %s"%x




                              # And now my version:

                              #============== =============== =============== ===
                              # main1.py
                              import main2

                              x = 23

                              def foo():
                              print "ENTERING main1.foo()"
                              import __main__ as main
                              global x
                              print "global x = %s"%main.x
                              main.x = 45
                              print "set main.x = %s"%main.x
                              print "global x = %s"%x
                              print "EXITING main1.foo()"

                              if __name__ == "__main__":
                              print "main1.x = %s"%x
                              foo()
                              main2.foo()
                              print "main1.x = %s"%x



                              #============== =============== =============== ===
                              # main2.py
                              x = 57

                              def foo():
                              print "ENTERING main2.foo()"
                              import __main__ as main
                              global x
                              print "global x = %s"%x
                              print "main2.x = %s"%main.x
                              main.x = 88
                              print "set main.x = %s"%main.x
                              print "global x = %s"%x
                              print "EXITING main2.foo()"

                              if __name__ == "__main__":
                              print "main2.x = %s"%x
                              foo()
                              print "main2.x = %s"%x



                              Thanks for your time,
                              Sean


                              Comment

                              Working...