Python Forum
use thing before self - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: use thing before self (/thread-34313.html)



use thing before self - sylvanas - Jul-17-2021

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!!!


RE: use thing before self - ndc85430 - Jul-17-2021

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?


RE: use thing before self - Yoriz - Jul-17-2021

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



RE: use thing before self - sylvanas - Jul-21-2021

(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