Access an object to which being bound
DL Neil
PythonList at DancesWithMice.info
Wed May 27 18:44:43 EDT 2020
@AR,
On 28/05/20 8:41 AM, Chris Angelico 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?
Here's some sample code, which may give you some ideas (intriguingly it
comes from my playing-about with a project @Chris described (some time
back), which includes the need to 'register' sub-components and to keep
a count of them):
# PSL
import collections
from itertools import count
class Link():
'''Contain original link, manipulate for presentation,
and retain for later action.
'''
ID = count( 0 )
instances = []
def __init__( self, token:Token, )->None:
'''Instantiate.'''
self.token = token
self.linkNR:int = next( self.ID ) #linkNR
Link.instances.append( self ) #form a register of instances/links
self.URL:str = ""
...more methods here - likely irrelevant to your needs...
class LinksRegister( collections.UserList ):
'''Links available for use within application.'''
def __iter__( self ):
'''Generator.'''
for link in self.data:
yield link
def register_links( self, cls ):
'''Register all links.'''
self.data = cls.instances
Once all of the Link() objects have been ascertained, we can make use of
the class-attributes:
links_register.register_links( Link )
NB in this scenario it is only necessary to register once - all of the
links at-once, cf registering each link as it is itself instantiated.
Also, that each Link() object is not aware that it is/will be 'registered'!
Later, when it is necessary to carry-out the same action on each of the
objects, we can use the register's iterator/generator (as above).
Critique/other ideas welcome...
--
Regards =dn
More information about the Python-list
mailing list