Access an object to which being bound

Chris Angelico rosuav at gmail.com
Wed May 27 16:41:35 EDT 2020


On Thu, May 28, 2020 at 6:27 AM Abdur-Rahmaan Janhangeer
<arj.python at gmail.com> wrote:
>
> Thanks,
>
> Actually i want to keep a reference from B to all A
> instantiated like in the case of z
>
> I have class A and i want to call class B via A
>
> You can have
>
> def x(self, *args, **kwargs):
>     return A(*args, **kwargs)
>
> but was wondering if we could keep track while
> doing it via z = ...
>

Okay, now I think I get what you're after. Let's simplify and clarify
things. Let me know if I'm misinterpreting.

1) You have a container class that can instantiate objects of a thing class
2a) You wish for the container to be aware of all things it has created - OR -
2b) You wish for the Thing to be aware of all containers that have created them
3) You want this to happen automatically.

I'm not sure which way round you're trying to do this, so I'll try to
answer both. Your Container (Hooman) can construct multiple Things
(Button), and other classes could also construct Things. So you need
some way to automatically register the Thing as you create it.

The easiest way to do this would, I think, be to have your Container
subclass a utility class, and then use self.Button() instead of
Button(). That can take care of registering the button in some sort of
list, and then it can still return the button in case you need it for
something else.

Would that work? Or am I completely off on my analysis?

ChrisA


More information about the Python-list mailing list