By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

Acceessing attribute from diffrent classes &diffrent modules

P: 6
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 !
Mar 19 '08 #1
Share this Question
Share on Google+
6 Replies


jlm699
100+
P: 314
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
Expand|Select|Wrap|Line Numbers
  1. class A (self): 
  2.     def AX (self):
  3.         x = 'somestring'
  4.         return x
  5.  
module2.py
Expand|Select|Wrap|Line Numbers
  1. import module1
  2.  
  3. class B( self, ....):
  4.     def AY (self, .....):
  5.         inst_ClassA = module1.A()
  6.         # inst_ClassA is now an instance of the class A from module1
  7.         retval = inst_ClassA.AX()
  8.         # The previous command invokes the class's method AX() 
  9.         print retval
But like you said, this is assuming the __init__ functions are defined properly
Mar 19 '08 #2

P: 6
Thanks for the Reply,

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

Thanks
Mar 19 '08 #3

jlm699
100+
P: 314
Well honestly for something so simple you don't need init functions. Modifying the previous code like this will perform the illustrated task:
Module1:
Expand|Select|Wrap|Line Numbers
  1. class A:
  2.     def AX (self):
  3.         x = 'somestring'
  4.         return x
Module2:
Expand|Select|Wrap|Line Numbers
  1. import module1
  2.  
  3. class B:
  4.     def AY (self):
  5.         inst_ClassA = module1.A()
  6.         # inst_ClassA is now an instance of the class A from module1
  7.         retval = inst_ClassA.AX()
  8.         # The previous command invokes the class's method AX() 
  9.         print retval
  10.  
  11. def main():
  12.     inst_ClassB = B()
  13.  
  14.     inst_ClassB.AY()
  15.  
  16.  
  17. if __name__ == '__main__':
  18.     main()
Output:
Expand|Select|Wrap|Line Numbers
  1. C:\Documents and Settings\Administrator\Desktop\pythtests>python module2.py
  2. somestring
  3.  
  4. 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.
http://www.penzilla.net/tutorials/python/classes/
Mar 19 '08 #4

Subsciber123
P: 87
Do you mean this?
Expand|Select|Wrap|Line Numbers
  1. class A:
  2.     def foo(self):
  3.         print "foo"
  4.  
  5. class B:
  6.     def bar(self):
  7.         print "bar"
  8.  
  9. b=B()
  10. A.foo(b)
Mar 21 '08 #5

P: 6
here's my problem :

i have 2 classes , with these instantiations

[PHP]Class A:
def __init__(self):
self.service = None
self.currentVerMap = 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.DefaultPosition,
size=(700,650), style=wx.DEFAULT_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,
Apr 10 '08 #6

jlm699
100+
P: 314
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.
Apr 16 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.