Access an object to which being bound

Abdur-Rahmaan Janhangeer arj.python at gmail.com
Wed May 27 16:57:30 EDT 2020


Hum yes a third class is a nice option!

btw managed to do it with those in Hooman

    self._all_widgets = []

    def button(self, *args, **kwargs):
        b = Button(*args, **kwargs)
        self._all_widgets.append(b)
        return b

    def update_all(self):
        for widget in self._all_widgets:
            widget.update()

Just in the drawing loop i have (hapi is a Hooman object)

for i in range(5):
        x = hapi.button(
        # params
        )
hapi.update_all()

the update all adds a button in the array each frame, that's beyond the
question
but for creating buttons on the fly each frame was never a good option
(better define
before then update x and y in loop)

that's the only caveat to watch out if people create objects on the fly

Kind Regards,

Abdur-Rahmaan Janhangeer
compileralchemy <https://compileralchemy.github.io/> | blog
<https://abdur-rahmaanj.github.io/>
github <https://github.com/Abdur-RahmaanJ>
Mauritius


On Thu, May 28, 2020 at 12:41 AM Chris Angelico <rosuav at gmail.com> wrote:

> 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
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list