Python Forum
How to pass variables from one class to another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass variables from one class to another
#1
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:

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?
Reply
#2
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
Reply
#3
(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...
Reply
#4
A: You can have an external party pass the information from one to the other.
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.
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).
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.
hobbyist likes this post
Reply
#5
(Sep-22-2021, 02:08 PM)deanhystad Wrote: A: You can have an external party pass the information from one to the other.
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.
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).
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() ?
Reply
#6
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.
Reply
#7
(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)
   
Reply
#8
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.
Reply
#9
Look at the below
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?
Reply
#10
If that is the problem I think I'll need to go break things. "Doctor, doctor, it hurts when I stab myself!"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 518 Nov-12-2021, 09:55 PM
Last Post: snippsat
  Acess variables from class samuelbachorik 3 569 Aug-20-2021, 02:55 PM
Last Post: deanhystad
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 1,335 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  Do I have to pass 85 variables to function? Milfredo 10 1,867 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 1,231 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Class variables menator01 2 899 Jun-04-2020, 04:23 PM
Last Post: Yoriz
  Question about naming variables in class methods sShadowSerpent 1 958 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Pass by reference vs Pass by value leodavinci1990 1 1,138 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  Understanding Class Variables vindo 9 2,001 Jun-05-2019, 08:04 PM
Last Post: Yoriz
  What is the strategy for working with class variables? AlekseyPython 3 1,658 Feb-24-2019, 05:34 AM
Last Post: AlekseyPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020