Posts: 144
Threads: 41
Joined: Jun 2020
Sep-22-2021, 11:39 AM
(This post was last modified: Sep-22-2021, 11:39 AM by hobbyist.)
How to pass values from one class to another when the class you want to get value from is after. What I mean: Suppose I have two classes:
1 2 3 4 5 6 7 |
class One:
.....
f = ClassTwoValue
class Two:
....
ClassTwoValue = 2
|
I want the class One to get the value from class Two. I found solutions (from google search, such as this: https://stackoverflow.com/questions/1999...to-another) only for class Two to get the value from class One... which does not work for my situation... Any ideas?
Posts: 6,779
Threads: 20
Joined: Feb 2020
Describe your problem, not the solution. Why do these classes need to know about each other? What is their relationship?
hobbyist and ndc85430 like this post
Posts: 144
Threads: 41
Joined: Jun 2020
Sep-22-2021, 12:42 PM
(This post was last modified: Sep-22-2021, 12:42 PM by hobbyist.)
(Sep-22-2021, 12:04 PM)deanhystad Wrote: Describe your problem, not the solution. Why do these classes need to know about each other? What is their relationship?
Class two has a value that I need to pass it to Class One.. otherwise, I should program the Class One to do the same operation as Class Two. For instance, suppose that class Two gets measurements from a supersonic sensor, I want to pass these measurements to class One... there is no other way... I am searching it all day...
Posts: 6,779
Threads: 20
Joined: Feb 2020
Sep-22-2021, 02:08 PM
(This post was last modified: Sep-22-2021, 02:08 PM by deanhystad.)
A: You can have an external party pass the information from one to the other.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class GetInterestingInfo():
def get_info( self ):
self .device.read_data()
self .process_raw_data()
return self .processed_data
class UseInterestingInfo():
def use_info( self , interesting_info):
pass
x = GetInterestingInfo()
y = UseInterestingInfo()
y.use_info(x.get_info()):
|
B: You could bind an instance of one to the other. This is just a cleaner version of A and is really common for GUI type classes where you want a generic way to make two objects communciate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class GetInterestingInfo():
def get_info( self ):
self .device.read_data()
self .process_raw_data()
return self .processed_data
class UseInterestingInfo():
def bind( self , func):
self .get_info_func = func
def use_info( self ):
updated_info = self .get_info_func()
x = GetInterestingInfo()
y = UseInterestingInfo()
y.bind(x.get_info)
|
C: You could have one of the classes create an instance of the other. Since most things in Python are objects you are already doing this all the the time. If class A knows it is going to use class B, have class A create an instance of class B and keep it as an instance variable (or maybe a class variable depending on your need).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class GetInterestingInfo():
def get_info( self ):
self .device.read_data()
self .process_raw_data()
return self .processed_data
class UseInterestingInfo():
def __init__( self ):
self .get_info = GetInterestingInfo()
def use_info( self ):
updated_info = self .get_info()
x = GetInterestingInfo()
|
D: Create instance of data class that is shared by the producer and consumer.
E: Use a database. Similar to D, but very generic.
F, G, H:... Lots of ways to solve this problem. The best answer is very dependent upon the relationship. There is no "one size fits all" solution.
Posts: 144
Threads: 41
Joined: Jun 2020
(Sep-22-2021, 02:08 PM)deanhystad Wrote: A: You can have an external party pass the information from one to the other.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class GetInterestingInfo():
def get_info( self ):
self .device.read_data()
self .process_raw_data()
return self .processed_data
class UseInterestingInfo():
def use_info( self , interesting_info):
pass
x = GetInterestingInfo()
y = UseInterestingInfo()
y.use_info(x.get_info()):
|
B: You could bind an instance of one to the other. This is just a cleaner version of A and is really common for GUI type classes where you want a generic way to make two objects communciate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class GetInterestingInfo():
def get_info( self ):
self .device.read_data()
self .process_raw_data()
return self .processed_data
class UseInterestingInfo():
def bind( self , func):
self .get_info_func = func
def use_info( self ):
updated_info = self .get_info_func()
x = GetInterestingInfo()
y = UseInterestingInfo()
y.bind(x.get_info)
|
C: You could have one of the classes create an instance of the other. Since most things in Python are objects you are already doing this all the the time. If class A knows it is going to use class B, have class A create an instance of class B and keep it as an instance variable (or maybe a class variable depending on your need).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class GetInterestingInfo():
def get_info( self ):
self .device.read_data()
self .process_raw_data()
return self .processed_data
class UseInterestingInfo():
def __init__( self ):
self .get_info = GetInterestingInfo()
def use_info( self ):
updated_info = self .get_info()
x = GetInterestingInfo()
|
D: Create instance of data class that is shared by the producer and consumer.
E: Use a database. Similar to D, but very generic.
F, G, H:... Lots of ways to solve this problem. The best answer is very dependent upon the relationship. There is no "one size fits all" solution.
Thanks! But, I need them, the other way... I need the data in class before I have them from the other class. It is odd, but in other languages such as C++ there is solution, I do not know how to achieve it in python. So, can the above code work if I have class UseInterestingInfo() first and then (after that) following the GetInterestingInfo() ?
Posts: 6,779
Threads: 20
Joined: Feb 2020
Sep-22-2021, 05:45 PM
(This post was last modified: Sep-22-2021, 05:45 PM by deanhystad.)
I don't understand what you are saying. Write a realistic example of what you want to do. If you can do that with C++, great. I know C++. If you can't, use python and pseudocode.
Posts: 144
Threads: 41
Joined: Jun 2020
Sep-22-2021, 06:04 PM
(This post was last modified: Sep-22-2021, 06:06 PM by hobbyist.)
(Sep-22-2021, 05:45 PM)deanhystad Wrote: I don't understand what you are saying. Write a realistic example of what you want to do. If you can do that with C++, great. I know C++. If you can't, use python and pseudocode.
So, the code goes from top to down, first reads class One, it reads the value, then reads the class after (class Two) and can pass the value from class One to class Two. This is what you showed me with your code and what I have found on google. That's fine...this is example 2, where the flow goes from up to down.
What I face in my code is the opposite (ex.1). The code reads first class One that needs read_distance value, but it does not have it because the class that does that work is class Two, which is after class One. The code will reach class Two, after class One. This is where I believe my code "explodes"...
What can I do?
In C++ you can fix that, I do not know how to fix it in python..
Attached Files
Thumbnail(s)
Posts: 6,779
Threads: 20
Joined: Feb 2020
Sep-22-2021, 07:01 PM
(This post was last modified: Sep-22-2021, 07:01 PM by deanhystad.)
Would you please just provide your Python code that is not working? Your explanations are confusing. I'm not going to respond anymore until I see code.
Posts: 1,358
Threads: 2
Joined: May 2019
Look at the below
1 2 3 4 5 6 7 8 9 10 11 |
class One:
def get_a_life( self ):
paris = Two.give_a_life( self )
return paris
class Two:
def give_a_life( self ):
return "What we will always have"
onesie = One()
print (onesie.get_a_life())
|
Output: What we will always have
Class One calls class Two before class Two has been defined. Is that what you are wanting?
Posts: 6,779
Threads: 20
Joined: Feb 2020
If that is the problem I think I'll need to go break things. "Doctor, doctor, it hurts when I stab myself!"
|