Is this possible? Or anything else can work in another way?
class A():
def __init__(self):
self.b=B()
def func_in_A(self):
#blablabla
class B():
def run(self):
self.higher_hierachy.func_in_A()
#where 'self.higher_hierachy==self in A'
The reason why I do this is I want the class of child thread (self.b) to use a function that is defined in the class of the main thread (A).
inherit A seems to be logically unnecessary and I don't know how to pass a func to self.b=B()
Thanks ahead!!!
It's not clear what you're trying to do really, but if
B
needs an instance of
A
to do its job, can't you just pass that to
__init__
and then call its methods as required? Something like
my_a = A()
my_b = B(my_a)
my_b.run()
where the classes look like:
class A(object):
def do_something(self):
pass
class B(object):
def __init__(self, an_a):
self.a = an_a
def run(self):
a.do_something()
That is, composition rather than inheritance. You've said inheritance was unnecessary, so why not composition?
Seems a bit weird but
class A:
def __init__(self):
self.b = B(self)
def func_in_a(self):
print('func_in_a')
class B:
def __init__(self, instance_of_a):
self.higher_hierachy = instance_of_a
def run(self):
self.higher_hierachy.func_in_a()
# where 'self.higher_hierachy==self in A'
instance_of_a = A()
instance_of_a.b.run()
Output:
func_in_a
(Jul-17-2021, 07:50 PM)ndc85430 Wrote: [ -> ]It's not clear what you're trying to do really, but if B
needs an instance of A
to do its job, can't you just pass that to __init__
and then call its methods as required? Something like
my_a = A()
my_b = B(my_a)
my_b.run()
where the classes look like:
class A(object):
def do_something(self):
pass
class B(object):
def __init__(self, an_a):
self.a = an_a
def run(self):
a.do_something()
That is, composition rather than inheritance. You've said inheritance was unnecessary, so why not composition?
(Jul-17-2021, 07:57 PM)Yoriz Wrote: [ -> ]Seems a bit weird but
class A:
def __init__(self):
self.b = B(self)
def func_in_a(self):
print('func_in_a')
class B:
def __init__(self, instance_of_a):
self.higher_hierachy = instance_of_a
def run(self):
self.higher_hierachy.func_in_a()
# where 'self.higher_hierachy==self in A'
instance_of_a = A()
instance_of_a.b.run()
Output:
func_in_a
Thanks I was trying to solve problems between a main thread and child thread, since i was using pyqt so i solved it with emitting signal in the end.
Thanks anyways