Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
use thing before self
#1
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!!!
Reply
#2
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?
Reply
#3
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
Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am confused with the key and value thing james1019 3 948 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Need help i just started the whole thing gabriel789 16 3,178 Sep-12-2022, 08:04 PM
Last Post: snippsat
  i making a terminal sign up website thing Kenrichppython 1 1,695 Nov-04-2021, 03:57 AM
Last Post: bowlofred
  Not able to make a specific thing pause in pygame cooImanreebro 4 3,199 Dec-13-2020, 10:34 PM
Last Post: cooImanreebro
  Help with list thing please Yeyzon 1 2,049 Apr-19-2019, 07:51 AM
Last Post: snippsat
  Printing one thing from a list bidoofis 1 2,336 Feb-05-2019, 09:02 PM
Last Post: ichabod801
  I found weird thing. catastrophe_K 1 2,091 Sep-29-2018, 09:59 AM
Last Post: gruntfutuk
  Make one thing act like another Zman350x 2 2,195 Aug-16-2018, 06:45 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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