Acceessing attribute from diffrent classes &diffrent modules

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GTAPy
    New Member
    • Mar 2008
    • 6

    Acceessing attribute from diffrent classes &diffrent modules

    Hello Python Dev.
    i am new to python ...i would like to know more about how to access attribute values within different classes, different modules ....Assum we have the following

    [HTML]Module 1
    Class A (self):
    def AX (self):
    x = 'somestring'
    return x

    Module 2
    Class B( self, ....):
    def AY (self, .....):
    [here i want to use x value
    from AX method in module 1][/HTML]

    the question is, how to inherit the x value from module 1 and use it in module 2 in AY method, assuming that __init__ is already defined for each class and being used by other methods too.....

    any help is appreciated ....

    Thanks !
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Originally posted by GTAPy
    the question is, how to inherit the x value from module 1 and use it in module 2 in AY method
    I think this is what you're asking...
    module1.py[CODE=python]
    class A (self):
    def AX (self):
    x = 'somestring'
    return x
    [/CODE]module2.py[CODE=python]
    import module1

    class B( self, ....):
    def AY (self, .....):
    inst_ClassA = module1.A()
    # inst_ClassA is now an instance of the class A from module1
    retval = inst_ClassA.AX( )
    # The previous command invokes the class's method AX()
    print retval[/CODE]
    But like you said, this is assuming the __init__ functions are defined properly

    Comment

    • GTAPy
      New Member
      • Mar 2008
      • 6

      #3
      Thanks for the Reply,

      but what it should be define in __Init__ , i mean what's the proper __init__ definition to do this ?

      Thanks

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        Well honestly for something so simple you don't need init functions. Modifying the previous code like this will perform the illustrated task:
        Module1:
        [code=python]
        class A:
        def AX (self):
        x = 'somestring'
        return x[/code]
        Module2:
        [code=python]
        import module1

        class B:
        def AY (self):
        inst_ClassA = module1.A()
        # inst_ClassA is now an instance of the class A from module1
        retval = inst_ClassA.AX( )
        # The previous command invokes the class's method AX()
        print retval

        def main():
        inst_ClassB = B()

        inst_ClassB.AY( )


        if __name__ == '__main__':
        main()[/code]
        Output:
        Code:
        C:\Documents and Settings\Administrator\Desktop\pythtests>python module2.py
        somestring
        
        C:\Documents and Settings\Administrator\Desktop\pythtests>
        If you are looking to do something more advanced with classes then refer to the following tolearn about the __init__ function etc.
        Atomic penguin schedules solid day of destruction

        Comment

        • Subsciber123
          New Member
          • Nov 2006
          • 87

          #5
          Do you mean this?
          [CODE=python]class A:
          def foo(self):
          print "foo"

          class B:
          def bar(self):
          print "bar"

          b=B()
          A.foo(b)[/CODE]

          Comment

          • GTAPy
            New Member
            • Mar 2008
            • 6

            #6
            here's my problem :

            i have 2 classes , with these instantiations

            [PHP]Class A:
            def __init__(self):
            self.service = None
            self.currentVer Map = None
            self.map= None
            self.Name1 = None
            self.Name2 = None
            .
            .
            .
            etc

            def AX (self, name):
            self.somevalue = gold
            return self.somevalue


            Class B (wx.Frame) :
            def __init__(self, parent, id=wx.ID_ANY, title='', pos=wx.DefaultP osition,
            size=(700,650), style=wx.DEFAUL T_FRAME_STYLE):

            wx.Frame.__init __(self, parent, id, title, pos, size, style)

            def __BY (self, somevalue):
            all i need inside this method is getting
            GOLD value from Class A, method AX and use it
            inside BY method
            [/PHP]

            plz guys .......i appreciate any help to solve this !

            Thanks,

            Comment

            • jlm699
              Contributor
              • Jul 2007
              • 314

              #7
              In order to use a member of the A class, you must pass an instance to the BY function.
              ie:
              __BY(inst_class _a)

              ... However, from what you are saying it doesn't seem that you fully understand the purpose of a class... Classes shouldn't ever depend on one another unless you're talking about inheritance or implementation.

              You need to further explain your problem because in its current state, it doesn't make any sense.

              Comment

              Working...