1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Rep Power

    Help Needed CLASSES and FUNCTIONS!!!

    I have a small problem I think the following code explains what I am trying to do:
    class TEST:
    def __init__ (self):
    print "INSTANCE"
    def A (self):
    self.a = 'AZ'
    def B (self):
    print self.a

    if __name__ == '__main__':

    test = TEST()
    I am trying to get the value of self.a in def B() though it is being declared in def A(). Currently it doesn't work and gets an error:
    AttributeError: Azeem instance has no attribute 'a'

    Now is there a way to get the value of self.a in def B() without declaring self.a in __init__() i.e. any way to get to that value using any class reference or anything.

    What I want to do is that create multiple lists at runtime in different methods. I am not sure ahead how many lists I need to create as it all depends on the data in the input-file. I have different methods that populate the relevant lists. That's a small picture of the code I am working. Any help is appreciated.
  2. #2
  3. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    A World of wonders.
    Rep Power
    This could work for ya
    class TEST:
        def __init__  ( self ):
            print "INSTANCE"
            self.A ( " " )
        def A ( self, var ):
            self.var    = 'AZ'
        def B ( self ):
            print self.var
    if __name__ == '__main__':
        test = TEST ( )
        test.B ( )
    Last edited by xlordt; May 26th, 2004 at 06:00 PM.

IMN logo majestic logo threadwatch logo seochat tools logo