Two classes problem

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

    #1

    Two classes problem

    I have two classes;

    a.py -->

    #!/usr/bin/python
    global test
    test =''
    class a(b):
    def __init__(self,t est):
    print test
    print 'Outside: '+test

    b.py -->

    #!/usr/bin/python
    import a
    a.a('Garry')

    I want to pass this string (or any object later) to a.py and that too
    outside the scope of class a.
    Is that possible??? If yes, how???

    Thanks,

    Garry
  • wittempj@hotmail.com

    #2
    Re: Two classes problem

    If you create a.py like
    -test = 'Spam'
    -
    -class a:
    - def __init__(self, arg):
    - global test
    - test = arg
    - self.result = arg + ' added'

    - def __call__(self):
    - return self.result
    and b.py like
    -import a
    -a.test = 'donkey'
    -x = a.a('dinosaur')
    -print a.test
    It will print 'dinosaur', so you changed the global variable. The thing
    is however that this is a highly confusing way of programming for me,
    so I would not use it just because it is possible

    Comment

    • Caleb Hattingh

      #3
      Re: Two classes problem

      Hi

      It would help if you could describe the purpose you have in mind for doing
      this. There is a cute way of doing what you want:

      ===file: a.py===
      # module a.py
      test = 'first'
      class aclass:
      def __init__(self, mod, value):
      mod.test = value # Is there another way to refer to
      the module this class sits in?
      ===end: a.py===

      ===file: b.py===
      # file b.py
      import a
      x = a.aclass(a,'mon key')
      print a.test
      ===end: b.py===

      When you run "b.py", you will see 'monkey' printed. What we have done
      here is passed the reference to the *module* a to the constructor for
      aclass, and that constructor modified the module variable "test". This is
      a way to avoid using 'global', or in other words, the namespaces of things
      are still clear (for me anyway).

      Cya
      Caleb



      On Thu, 3 Feb 2005 00:13:05 +0530, Gurpreet Sachdeva
      <gurpreet.sachd eva@gmail.com> wrote:
      [color=blue]
      > I have two classes;
      >
      > a.py -->
      >
      > #!/usr/bin/python
      > global test
      > test =''
      > class a(b):
      > def __init__(self,t est):
      > print test
      > print 'Outside: '+test
      >
      > b.py -->
      >
      > #!/usr/bin/python
      > import a
      > a.a('Garry')
      >
      > I want to pass this string (or any object later) to a.py and that too
      > outside the scope of class a.
      > Is that possible??? If yes, how???
      >
      > Thanks,
      >
      > Garry[/color]

      Comment

      • Steven Bethard

        #4
        Re: Two classes problem

        Caleb Hattingh wrote:[color=blue]
        > ===file: a.py===
        > # module a.py
        > test = 'first'
        > class aclass:
        > def __init__(self, mod, value):
        > mod.test = value # Is there another way to refer
        > to the module this class sits in?
        > ===end: a.py===[/color]

        You can usually import the current module with:

        __import__(__na me__)

        so you could write the code like:

        test = 'first'
        class aclass:
        def __init__(self, value):
        mod = __import__(__na me__)
        mod.test = value

        or you could use globals() like:

        test = 'first'
        class aclass:
        def __init__(self, value):
        globals()['test'] = value
        [color=blue]
        > ===file: b.py===
        > # file b.py
        > import a
        > x = a.aclass(a,'mon key')
        > print a.test
        > ===end: b.py===[/color]

        If you used one of the solutions above, this code would be rewritten as:

        import a
        x = a.aclass('monke y')
        print a.test


        To the OP:

        In general, this seems like a bad organization strategy for your code.
        What is your actual use case?

        Steve

        Comment

        • Caleb Hattingh

          #5
          Re: Two classes problem

          Steven, thanks for your help once again :)
          [color=blue]
          > so you could write the code like:
          >
          > test = 'first'
          > class aclass:
          > def __init__(self, value):
          > mod = __import__(__na me__)
          > mod.test = value[/color]

          This is sweet. I really like this technique for manipulating module-scope
          identifiers (from within a class or function).
          [color=blue]
          > To the OP:
          >
          > In general, this seems like a bad organization strategy for your code.
          > What is your actual use case?[/color]

          Agreed. It is an interesting intellectual exercise, but there is surely a
          better way of controlling access to 'test' in the OP's post.

          thx
          Caleb

          Comment

          • Gurpreet Sachdeva

            #6
            Re: Two classes problem

            The purpose is, I pass a list to a class in a module but I want to use
            that list out of the scope of that class and that too not in any other
            class or a function but in the main program...
            The problem is that when I import that, the statements in the module
            which are not in the class are executed first and then the variable
            gets intiallised...
            I will explain with the example...

            -global test
            -
            -class a:
            - def __init__(self,t est):
            - global test
            - print test
            -
            -print 'Outside: '+test

            I want to print that variable test which I am giving to the class as
            an argument, in the scope of main...
            I know it is not a good way of programming but my situation is like this...
            But is this possible or not? If I pass test as 'Garry' can I (by any
            way) print 'Outside: Garry' with that print statement... (in the main
            scope)

            Thanks and Regards,
            Garry

            Comment

            • Steven Bethard

              #7
              Re: Two classes problem

              Gurpreet Sachdeva wrote:[color=blue]
              > The purpose is, I pass a list to a class in a module but I want to use
              > that list out of the scope of that class and that too not in any other
              > class or a function but in the main program...
              > The problem is that when I import that, the statements in the module
              > which are not in the class are executed first and then the variable
              > gets intiallised...
              > I will explain with the example...
              >
              > -global test
              > -
              > -class a:
              > - def __init__(self,t est):
              > - global test
              > - print test
              > -
              > -print 'Outside: '+test
              >
              > I want to print that variable test which I am giving to the class as
              > an argument, in the scope of main...
              > I know it is not a good way of programming but my situation is like this...
              > But is this possible or not? If I pass test as 'Garry' can I (by any
              > way) print 'Outside: Garry' with that print statement... (in the main
              > scope)[/color]

              Probably a better approach for this would be something like:

              class A(object):
              def __init__(self, test):
              self.test = test
              def printtest(self) :
              print 'Outside: %s' % self.test

              where your code in your main module looks something like:

              import amodule
              a = amodule.A('Garr y')
              a.printtest()

              The __init__ method is used for initializing *instances* of a class, so
              using the __init__ method to initialize a module-level variable doesn't
              really make much sense. If you really want to play around with
              module-level variables, you probably want to modify them with
              module-level functions, e.g.:

              test = 'default'
              def a(val):
              global test
              test = val
              def printtest():
              print 'Outside: %s' % test

              where your code in your main module looks something like:

              import amodule
              amodule.a('Garr y')
              amodule.printte st()

              Note that I've moved all your code that would normally be executed at
              module import time into the 'printtest' function. This way you can
              execute this code after import.

              If you really need to have the code executed at the same time as the
              module is imported, another option would be to exec your code in an
              empty module instead of importing it:

              py> a_module_str = """print 'Outside: %s' % test"""
              py> a_module = new.module('a')
              py> a_module.test = 'Garry'
              py> exec a_module_str in a_module.__dict __
              Outside: Garry

              Here I create an empty module, set its 'test' attribute to 'Garry', and
              then execute the rest of your code (e.g. the print statement). This is
              certainly my least favorite approach, but it seems to do the closest
              thing to what you're asking for.

              Steve

              Comment

              Working...