How do i reach a class from within another class?

I have
Class A and
Class B

How can i activate or run class A from Class B?

example, if class A and class B work toward the same resource, one create data the other consume data. How can i make class A creator start again and create more data?

Something like this may do ...

class A:
    def __init__(self, num=1):
        self.strA = '<>' * num

class B:
    def __init__(self):
        # create an instance of class A within class B
        a = A()
        print(a.strA)  # <>
        a = A(3)
        print(a.strA)  # <><><>
        a = A(5)
        print(a.strA)  # <><><><><>

b = B()

Edited 6 Years Ago by vegaseat: instance

Remember that i am a newbie.

I didnt understand that example, i will post some code here that i have worked with, i need help here please. I have a problem that i cant solve.

How do i make the C wait or hinder it from start if there is a chance that data_list will run dry? less then 0

Keep it simple please and i dont want to use a que if that is possible. I have worked with this many days and i cant fix it.

import time
import random
from threading import Thread, Lock


def start():
    
    global lock		
    global data_list  
    
    lock = Lock()
    
    data_list = 0		

    thread1 = P()
    thread1.start()
    
    thread2 = C()
    thread2.start()
    

class P(Thread):
    
	def run(self):
	
	    global data_list
	    
	    lock.acquire()
	

	
	    
	    n = random.randint(0, 99)
	    print n, 
	    
	    while n >= 0:
		x = random.randint(0,99) 
		print x,
		data_list = data_list +x
		time.sleep(.30) 
		n = n-1
	    
	    
	    print "-"*60
	    
	    print( data_list ),
	    
	
	    lock.release()
	

    
class C(Thread):
    
   
    	def run(self):
	
	    global data_list
	
	    lock.acquire()
	
	    
	    print "-"*60
	    
	
	    m = random.randint(0, 99)
	    
	    print m,
	    
	    
	    while m >= 0:
		x = random.randint(0,99)
		print x,	
		data_list = data_list -x
		time.sleep(.15)
		m = m-1
		
	    print ""
	    print "-"*60
	  
	    print( data_list ),
		
		
	    lock.release()
	
	
if __name__ == '__start__':
	start()

Edited 6 Years Ago by Godflesh: n/a

First of all, you are mixing tabs and spaces in your code indentations. The program editor I am using absolutely hates this. Please stick with the customary 4 spaces, you will gain from it in the long run.

Also, since your data_list is not a list there is no chance that it will "run dry", or is data_list just a poorly named variable?

What is this code supposed to accomplish?
What does it have to do with the title of this thread?

Edited 6 Years Ago by vegaseat: n/a

First of all, you are mixing tabs and spaces in your code indentations. The program editor I am using absolutely hates this. Please stick with the customary 4 spaces, you will gain from it in the long run.

Also, since your data_list is not a list there is no chance that it will "run dry", or is data_list just a poorly named variable?

What is this code supposed to accomplish?
What does it have to do with the title of this thread?

It have nothing to do with the title i guess, except maybe how you make a function in class c that will start the class/thread p.

The program are supposed to create data in a global variable with p thread. The c thread are supposed to remove data from the same variable...the problem i have :

How do you stop the c thread from removing data / subtract data if from getting a negative value = variable empty. Its hard for me to explain.

Are you describing a "race condition"? If you are working with something like this you are not a newbie, this is complex stuff.

try googling "data race condition" and learn about the different methods to solve this fundamental software design issue.

mhh!... I see only two ways of mixing two classes. Making an instance of Class A in class B as Vega have already done or Inherit class A into B and use the methods as Local to the class.

The program are supposed to create data in a global variable with p thread. The c thread are supposed to remove data from the same variable...the problem i have :

How do you stop the c thread from removing data / subtract data if from getting a negative value = variable empty. Its hard for me to explain.

+1 for jlm699
These are'nt new bee stuffs!

This question has already been answered. Start a new discussion instead.