473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Acceessing attribute from diffrent classes &diffrent modules

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
6 1354
jlm699
314 100+
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
GTAPy
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
314 100+
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
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
GTAPy
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
314 100+
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

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

Similar topics

8
by: Corey Lubin | last post by:
someGlobal=1 class Original: def foo(self): # Make use of someGlobal from original import * someGlobal=2
8
by: Rob Snyder | last post by:
Greetings - I have a situation where I need to be able to have a Python function that will take all the modules in a given directory and find all the classes defined in these modules by name....
0
by: david | last post by:
I'm designing a web application - several web forms. I'm using components. I've also considering using modules for the development process too. With a module, I can define some public...
0
by: nntp.microsoft.com | last post by:
Anyone aware of any ready made C# classes/modules for processing credit cards for Authorize.net and shipping method calculation for UPS?
0
by: david | last post by:
I'm designing a web application - several web forms. I'm using components. I've also considering using modules for the development process too. With a module, I can define some public...
3
by: Rob R. Ainscough | last post by:
I think I've found what appears to be a pretty significant bug in VS.NET 2003. I deleted a module from my Project, and got the message "mymodule.vb will be deleted permanently" -- hit Ok. Bring...
3
by: Jon | last post by:
I have an xml document like so... <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:ng="http://newsgator.com/schema/extensions"> <channel> <title></title> <link></link>...
173
by: Zytan | last post by:
I've read the docs on this, but one thing was left unclear. It seems as though a Module does not have to be fully qualified. Is this the case? I have source that apparently shows this. Are...
4
by: =?Utf-8?B?MjJQb20=?= | last post by:
Hi All, This is all new to me so please be patient with me. What I have is a very large 'Al-In-One' program, not yet complete, that has over 70 Forms/Modules/Classes in it and needs to be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.