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

    Join Date
    Apr 2004
    Posts
    101
    Rep Power
    10

    Help Needed CLASSES and FUNCTIONS!!!


    hi,
    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()
    test.B()
    ******************************************
    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.
    Thanks
  2. #2
  3. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    This could work for ya
    Code:
    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