Is it possible to refer to the owner class that an object belongs to as an attribute?
Clash Royale CLAN TAG#URR8PPP
up vote
10
down vote
favorite
I am not quite sure this is possible (or something similar) in python. I want to access a method (or another object) of a class from an object that is an attribute of such class.
Consider the following code:
class A():
def __init__(self):
self.b = B()
self.c = C()
def print_owner(self):
print('owner')
class B():
def __init__(self):
pass
def call_owner(self):
self.owner().print_owner()
so that b
as an object attribute of class A
, can refer to a method or attribute of A
?
Or similarly, is it possible that b
can access c
?
python
add a comment |Â
up vote
10
down vote
favorite
I am not quite sure this is possible (or something similar) in python. I want to access a method (or another object) of a class from an object that is an attribute of such class.
Consider the following code:
class A():
def __init__(self):
self.b = B()
self.c = C()
def print_owner(self):
print('owner')
class B():
def __init__(self):
pass
def call_owner(self):
self.owner().print_owner()
so that b
as an object attribute of class A
, can refer to a method or attribute of A
?
Or similarly, is it possible that b
can access c
?
python
1
self.owner
makes much more sense thanself.owner()
here.
â Mad Physicist
Aug 31 at 6:32
1
Consider modifying theWeakAttribute
example code from PEP 487. (If you're not using python 3.6, it's still possible with metaclasses.)
â o11c
Aug 31 at 7:05
add a comment |Â
up vote
10
down vote
favorite
up vote
10
down vote
favorite
I am not quite sure this is possible (or something similar) in python. I want to access a method (or another object) of a class from an object that is an attribute of such class.
Consider the following code:
class A():
def __init__(self):
self.b = B()
self.c = C()
def print_owner(self):
print('owner')
class B():
def __init__(self):
pass
def call_owner(self):
self.owner().print_owner()
so that b
as an object attribute of class A
, can refer to a method or attribute of A
?
Or similarly, is it possible that b
can access c
?
python
I am not quite sure this is possible (or something similar) in python. I want to access a method (or another object) of a class from an object that is an attribute of such class.
Consider the following code:
class A():
def __init__(self):
self.b = B()
self.c = C()
def print_owner(self):
print('owner')
class B():
def __init__(self):
pass
def call_owner(self):
self.owner().print_owner()
so that b
as an object attribute of class A
, can refer to a method or attribute of A
?
Or similarly, is it possible that b
can access c
?
python
python
edited Aug 31 at 6:38
Mad Physicist
29.7k156085
29.7k156085
asked Aug 31 at 6:11
edgarbc
837
837
1
self.owner
makes much more sense thanself.owner()
here.
â Mad Physicist
Aug 31 at 6:32
1
Consider modifying theWeakAttribute
example code from PEP 487. (If you're not using python 3.6, it's still possible with metaclasses.)
â o11c
Aug 31 at 7:05
add a comment |Â
1
self.owner
makes much more sense thanself.owner()
here.
â Mad Physicist
Aug 31 at 6:32
1
Consider modifying theWeakAttribute
example code from PEP 487. (If you're not using python 3.6, it's still possible with metaclasses.)
â o11c
Aug 31 at 7:05
1
1
self.owner
makes much more sense than self.owner()
here.â Mad Physicist
Aug 31 at 6:32
self.owner
makes much more sense than self.owner()
here.â Mad Physicist
Aug 31 at 6:32
1
1
Consider modifying the
WeakAttribute
example code from PEP 487. (If you're not using python 3.6, it's still possible with metaclasses.)â o11c
Aug 31 at 7:05
Consider modifying the
WeakAttribute
example code from PEP 487. (If you're not using python 3.6, it's still possible with metaclasses.)â o11c
Aug 31 at 7:05
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
10
down vote
accepted
It's possible. You can pass a reference to A to B constructor:
...
self.b = B(self)
...
class B:
def __init__(self, a):
self.a = a
So, B.a stores the reference to its owner A.
4
self.owner
instead ofself.a
according to OP, but the concept is 100% sound.
â Mad Physicist
Aug 31 at 6:33
add a comment |Â
up vote
7
down vote
There can be many references to object B()
, not only the one in instance of class A
. So it's not possible as it is in your code. (Well you could try a hack, like finding all instances of class A
in memory and find the one whose attribute b
points to your B instance, but that's a really bad idea).
You should explicitly store in instance of B
a reference to the owner.
add a comment |Â
up vote
2
down vote
You have a couple of options here. The better one is probably @Sianur suggests. It's simple, effective, and explicit. Give that answer an upvote.
Another option is to have the owner force itself on its minions. B
can do something like
def call_owner(self):
if hasattr(self, 'owner'):
self.owner().print_owner()
else:
print('I am free!')
Meanwhile, A
would set the owner attribute to itself:
def __init__(self):
self.b = B()
self.c = C()
self.b.owner = self.c.owner = self
In any case, if you want an object to have access to another object, store the reference into an accessible place. There's no magic here.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
It's possible. You can pass a reference to A to B constructor:
...
self.b = B(self)
...
class B:
def __init__(self, a):
self.a = a
So, B.a stores the reference to its owner A.
4
self.owner
instead ofself.a
according to OP, but the concept is 100% sound.
â Mad Physicist
Aug 31 at 6:33
add a comment |Â
up vote
10
down vote
accepted
It's possible. You can pass a reference to A to B constructor:
...
self.b = B(self)
...
class B:
def __init__(self, a):
self.a = a
So, B.a stores the reference to its owner A.
4
self.owner
instead ofself.a
according to OP, but the concept is 100% sound.
â Mad Physicist
Aug 31 at 6:33
add a comment |Â
up vote
10
down vote
accepted
up vote
10
down vote
accepted
It's possible. You can pass a reference to A to B constructor:
...
self.b = B(self)
...
class B:
def __init__(self, a):
self.a = a
So, B.a stores the reference to its owner A.
It's possible. You can pass a reference to A to B constructor:
...
self.b = B(self)
...
class B:
def __init__(self, a):
self.a = a
So, B.a stores the reference to its owner A.
answered Aug 31 at 6:23
Sianur
554412
554412
4
self.owner
instead ofself.a
according to OP, but the concept is 100% sound.
â Mad Physicist
Aug 31 at 6:33
add a comment |Â
4
self.owner
instead ofself.a
according to OP, but the concept is 100% sound.
â Mad Physicist
Aug 31 at 6:33
4
4
self.owner
instead of self.a
according to OP, but the concept is 100% sound.â Mad Physicist
Aug 31 at 6:33
self.owner
instead of self.a
according to OP, but the concept is 100% sound.â Mad Physicist
Aug 31 at 6:33
add a comment |Â
up vote
7
down vote
There can be many references to object B()
, not only the one in instance of class A
. So it's not possible as it is in your code. (Well you could try a hack, like finding all instances of class A
in memory and find the one whose attribute b
points to your B instance, but that's a really bad idea).
You should explicitly store in instance of B
a reference to the owner.
add a comment |Â
up vote
7
down vote
There can be many references to object B()
, not only the one in instance of class A
. So it's not possible as it is in your code. (Well you could try a hack, like finding all instances of class A
in memory and find the one whose attribute b
points to your B instance, but that's a really bad idea).
You should explicitly store in instance of B
a reference to the owner.
add a comment |Â
up vote
7
down vote
up vote
7
down vote
There can be many references to object B()
, not only the one in instance of class A
. So it's not possible as it is in your code. (Well you could try a hack, like finding all instances of class A
in memory and find the one whose attribute b
points to your B instance, but that's a really bad idea).
You should explicitly store in instance of B
a reference to the owner.
There can be many references to object B()
, not only the one in instance of class A
. So it's not possible as it is in your code. (Well you could try a hack, like finding all instances of class A
in memory and find the one whose attribute b
points to your B instance, but that's a really bad idea).
You should explicitly store in instance of B
a reference to the owner.
edited Aug 31 at 7:04
answered Aug 31 at 6:30
warvariuc
34.2k23116175
34.2k23116175
add a comment |Â
add a comment |Â
up vote
2
down vote
You have a couple of options here. The better one is probably @Sianur suggests. It's simple, effective, and explicit. Give that answer an upvote.
Another option is to have the owner force itself on its minions. B
can do something like
def call_owner(self):
if hasattr(self, 'owner'):
self.owner().print_owner()
else:
print('I am free!')
Meanwhile, A
would set the owner attribute to itself:
def __init__(self):
self.b = B()
self.c = C()
self.b.owner = self.c.owner = self
In any case, if you want an object to have access to another object, store the reference into an accessible place. There's no magic here.
add a comment |Â
up vote
2
down vote
You have a couple of options here. The better one is probably @Sianur suggests. It's simple, effective, and explicit. Give that answer an upvote.
Another option is to have the owner force itself on its minions. B
can do something like
def call_owner(self):
if hasattr(self, 'owner'):
self.owner().print_owner()
else:
print('I am free!')
Meanwhile, A
would set the owner attribute to itself:
def __init__(self):
self.b = B()
self.c = C()
self.b.owner = self.c.owner = self
In any case, if you want an object to have access to another object, store the reference into an accessible place. There's no magic here.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You have a couple of options here. The better one is probably @Sianur suggests. It's simple, effective, and explicit. Give that answer an upvote.
Another option is to have the owner force itself on its minions. B
can do something like
def call_owner(self):
if hasattr(self, 'owner'):
self.owner().print_owner()
else:
print('I am free!')
Meanwhile, A
would set the owner attribute to itself:
def __init__(self):
self.b = B()
self.c = C()
self.b.owner = self.c.owner = self
In any case, if you want an object to have access to another object, store the reference into an accessible place. There's no magic here.
You have a couple of options here. The better one is probably @Sianur suggests. It's simple, effective, and explicit. Give that answer an upvote.
Another option is to have the owner force itself on its minions. B
can do something like
def call_owner(self):
if hasattr(self, 'owner'):
self.owner().print_owner()
else:
print('I am free!')
Meanwhile, A
would set the owner attribute to itself:
def __init__(self):
self.b = B()
self.c = C()
self.b.owner = self.c.owner = self
In any case, if you want an object to have access to another object, store the reference into an accessible place. There's no magic here.
edited Aug 31 at 10:18
BartoszKP
26.2k1062100
26.2k1062100
answered Aug 31 at 6:34
Mad Physicist
29.7k156085
29.7k156085
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52109456%2fis-it-possible-to-refer-to-the-owner-class-that-an-object-belongs-to-as-an-attri%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
self.owner
makes much more sense thanself.owner()
here.â Mad Physicist
Aug 31 at 6:32
1
Consider modifying the
WeakAttribute
example code from PEP 487. (If you're not using python 3.6, it's still possible with metaclasses.)â o11c
Aug 31 at 7:05