Is it possible to refer to the owner class that an object belongs to as an attribute?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question



















  • 1




    self.owner makes much more sense than self.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















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?










share|improve this question



















  • 1




    self.owner makes much more sense than self.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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 than self.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













  • 1




    self.owner makes much more sense than self.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








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













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.






share|improve this answer
















  • 4




    self.owner instead of self.a according to OP, but the concept is 100% sound.
    – Mad Physicist
    Aug 31 at 6:33

















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.






share|improve this answer





























    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.






    share|improve this answer






















      Your Answer





      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      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






























      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.






      share|improve this answer
















      • 4




        self.owner instead of self.a according to OP, but the concept is 100% sound.
        – Mad Physicist
        Aug 31 at 6:33














      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.






      share|improve this answer
















      • 4




        self.owner instead of self.a according to OP, but the concept is 100% sound.
        – Mad Physicist
        Aug 31 at 6:33












      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.






      share|improve this answer












      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.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Aug 31 at 6:23









      Sianur

      554412




      554412







      • 4




        self.owner instead of self.a according to OP, but the concept is 100% sound.
        – Mad Physicist
        Aug 31 at 6:33












      • 4




        self.owner instead of self.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












      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.






      share|improve this answer


























        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.






        share|improve this answer
























          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 31 at 7:04

























          answered Aug 31 at 6:30









          warvariuc

          34.2k23116175




          34.2k23116175




















              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.






              share|improve this answer


























                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.






                share|improve this answer
























                  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.






                  share|improve this answer














                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 31 at 10:18









                  BartoszKP

                  26.2k1062100




                  26.2k1062100










                  answered Aug 31 at 6:34









                  Mad Physicist

                  29.7k156085




                  29.7k156085



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      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













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay