If I have the following code
class One:
scope = 'first_scope'
class Two:
scope = 'second_scope'
contained_object = One()
Is it possible for me given a reference to contained_object to determine through reflection whether it and the object referencing it have the same scope?
Thanks
EDIT: Apologies if the question was unclear, I wasn’t quite sure in python terminology how to ask it. I’ve contrived a sort of sample
An example might be
def sample(input):
#code in here to find out if input.scope
# matches a.scope without having a reference to it
a = Two()
a.scope = 'first scope'
a.contained_object.scope = 'will not match'
sample(a.contained_object)
It’s doable, you can do something like:
However, there are two more problems:
1. Why do you want to do it at the first place? I have a very bad feeling about this, unless you have a very good reason, otherwise, i will never suggest you to do it.
2. You need to figure out how to pick the right object when it has multiple referrers –
get_referrers()return a list of dicts representing all the objects referring the given one.You need to describe your question more specifically to get a better answer.