get object on which a method was called in Python





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Is there a way in Python to get a reference to an object on which a method was called?



And in case it is, is it possible even in a nested way?



my_class.py:



from modules import math_ops

class A():
def __init__(self):
self.math_ops = math_ops.B()
self.number = 1


modules/math_ops.py:



class B():
def add_1():
where_to_add = # Get instance of A() object
where_to_add.number += 1


To execute this:



>>> a = A()
>>> a.math_ops.add_1()


And get this:



>>> a.number
2


I'm asking because I am interested in writing a static method which works with the object on which it was called, but would like to avoid using the object as an argument as it would be much nicer to call a method like my_object.prop.static_method() instead of my_object.prop.static_method(my_object).










share|improve this question




















  • 2





    If your static method needs the object on which it is called, why not just use a regular instance method?

    – merlyn
    Nov 16 '18 at 10:59











  • If you mean using self in B.add_1(), then it would refer to the instance of B (e.g. A.math_ops) and not to the instance of A, I think.

    – pesekon2
    Nov 16 '18 at 12:27











  • Then I guess the statement you have a problem with is not my_object.static_method(my_object), but rather my_object.prop.static_method(my_object). Is that correct?

    – merlyn
    Nov 16 '18 at 12:40











  • Ouch. Right, I forgot to include it there. Sorry. Edited.

    – pesekon2
    Nov 16 '18 at 12:43


















0















Is there a way in Python to get a reference to an object on which a method was called?



And in case it is, is it possible even in a nested way?



my_class.py:



from modules import math_ops

class A():
def __init__(self):
self.math_ops = math_ops.B()
self.number = 1


modules/math_ops.py:



class B():
def add_1():
where_to_add = # Get instance of A() object
where_to_add.number += 1


To execute this:



>>> a = A()
>>> a.math_ops.add_1()


And get this:



>>> a.number
2


I'm asking because I am interested in writing a static method which works with the object on which it was called, but would like to avoid using the object as an argument as it would be much nicer to call a method like my_object.prop.static_method() instead of my_object.prop.static_method(my_object).










share|improve this question




















  • 2





    If your static method needs the object on which it is called, why not just use a regular instance method?

    – merlyn
    Nov 16 '18 at 10:59











  • If you mean using self in B.add_1(), then it would refer to the instance of B (e.g. A.math_ops) and not to the instance of A, I think.

    – pesekon2
    Nov 16 '18 at 12:27











  • Then I guess the statement you have a problem with is not my_object.static_method(my_object), but rather my_object.prop.static_method(my_object). Is that correct?

    – merlyn
    Nov 16 '18 at 12:40











  • Ouch. Right, I forgot to include it there. Sorry. Edited.

    – pesekon2
    Nov 16 '18 at 12:43














0












0








0








Is there a way in Python to get a reference to an object on which a method was called?



And in case it is, is it possible even in a nested way?



my_class.py:



from modules import math_ops

class A():
def __init__(self):
self.math_ops = math_ops.B()
self.number = 1


modules/math_ops.py:



class B():
def add_1():
where_to_add = # Get instance of A() object
where_to_add.number += 1


To execute this:



>>> a = A()
>>> a.math_ops.add_1()


And get this:



>>> a.number
2


I'm asking because I am interested in writing a static method which works with the object on which it was called, but would like to avoid using the object as an argument as it would be much nicer to call a method like my_object.prop.static_method() instead of my_object.prop.static_method(my_object).










share|improve this question
















Is there a way in Python to get a reference to an object on which a method was called?



And in case it is, is it possible even in a nested way?



my_class.py:



from modules import math_ops

class A():
def __init__(self):
self.math_ops = math_ops.B()
self.number = 1


modules/math_ops.py:



class B():
def add_1():
where_to_add = # Get instance of A() object
where_to_add.number += 1


To execute this:



>>> a = A()
>>> a.math_ops.add_1()


And get this:



>>> a.number
2


I'm asking because I am interested in writing a static method which works with the object on which it was called, but would like to avoid using the object as an argument as it would be much nicer to call a method like my_object.prop.static_method() instead of my_object.prop.static_method(my_object).







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 13:20







pesekon2

















asked Nov 16 '18 at 10:57









pesekon2pesekon2

254




254








  • 2





    If your static method needs the object on which it is called, why not just use a regular instance method?

    – merlyn
    Nov 16 '18 at 10:59











  • If you mean using self in B.add_1(), then it would refer to the instance of B (e.g. A.math_ops) and not to the instance of A, I think.

    – pesekon2
    Nov 16 '18 at 12:27











  • Then I guess the statement you have a problem with is not my_object.static_method(my_object), but rather my_object.prop.static_method(my_object). Is that correct?

    – merlyn
    Nov 16 '18 at 12:40











  • Ouch. Right, I forgot to include it there. Sorry. Edited.

    – pesekon2
    Nov 16 '18 at 12:43














  • 2





    If your static method needs the object on which it is called, why not just use a regular instance method?

    – merlyn
    Nov 16 '18 at 10:59











  • If you mean using self in B.add_1(), then it would refer to the instance of B (e.g. A.math_ops) and not to the instance of A, I think.

    – pesekon2
    Nov 16 '18 at 12:27











  • Then I guess the statement you have a problem with is not my_object.static_method(my_object), but rather my_object.prop.static_method(my_object). Is that correct?

    – merlyn
    Nov 16 '18 at 12:40











  • Ouch. Right, I forgot to include it there. Sorry. Edited.

    – pesekon2
    Nov 16 '18 at 12:43








2




2





If your static method needs the object on which it is called, why not just use a regular instance method?

– merlyn
Nov 16 '18 at 10:59





If your static method needs the object on which it is called, why not just use a regular instance method?

– merlyn
Nov 16 '18 at 10:59













If you mean using self in B.add_1(), then it would refer to the instance of B (e.g. A.math_ops) and not to the instance of A, I think.

– pesekon2
Nov 16 '18 at 12:27





If you mean using self in B.add_1(), then it would refer to the instance of B (e.g. A.math_ops) and not to the instance of A, I think.

– pesekon2
Nov 16 '18 at 12:27













Then I guess the statement you have a problem with is not my_object.static_method(my_object), but rather my_object.prop.static_method(my_object). Is that correct?

– merlyn
Nov 16 '18 at 12:40





Then I guess the statement you have a problem with is not my_object.static_method(my_object), but rather my_object.prop.static_method(my_object). Is that correct?

– merlyn
Nov 16 '18 at 12:40













Ouch. Right, I forgot to include it there. Sorry. Edited.

– pesekon2
Nov 16 '18 at 12:43





Ouch. Right, I forgot to include it there. Sorry. Edited.

– pesekon2
Nov 16 '18 at 12:43












2 Answers
2






active

oldest

votes


















1














If you never plan on reassigning math_ops outside A, this is fairly simple to do.



from modules import math_ops

class A():
def __init__():
self.math_ops = math_ops.B(self)
self.number = 1


modules/math_ops.py:



class B():
def __init__(self, creator):
self.creator = creator

def add_1():
creator.number += 1


I will mention it again in case you skimmed the first line, the following will generate unexpected results since B is tracking the creator of the object rather than the caller.



a1 = A()
a2 = A()
a1.math_ops = a2.math_ops
a1.math_ops.add_1() # a2 is updated


If that looks like something you might wanna do, the answer is a tad more complicated. Here's my attempt:



from modules import math_ops
class A():
def __init__(self):
self._math_ops = math_ops.B(self)
self.number = 1

@property
def math_ops(self):
self._math_ops.set_caller(self)
return self._math_ops

@math_ops.setter
def math_ops(self, new_math_ops):
self._math_ops = new_math_ops


modules/math_ops.py:



class B():
def __init__(self, caller):
self.caller = caller

def set_caller(self, caller):
self.caller = caller

def add_1(self):
self.caller.number += 1





share|improve this answer
























  • Looks good, but one question: Wouldn't it create a circular link and therefore also a possibility for memory leaks to keep the reference to the A object in B as we are also keeping a reference to B from A?

    – pesekon2
    Nov 16 '18 at 13:52











  • @pesekon2 Not if they don't have delete methods. See this answer.

    – merlyn
    Nov 16 '18 at 14:01











  • Oh, I see. This is for sure the correct answer, just one more question before accepting it: The memory is not released immediately and then when I am creating in a loop a lot of big objects reqriting each other (e.g. in each loop is a1 = A()), I can run out of memory when not using gc.collect(). Is there another way to avoid it besides including it in the property definition?

    – pesekon2
    Nov 19 '18 at 14:37











  • @pesekon2 You don't have to use gc.collect(). The gc in python is enabled by default. It will collect unclaimed objects automatically before you run out of memory.

    – merlyn
    Nov 19 '18 at 15:05



















-1














class A():
number = 1

class B():
def add_1():
where_to_add = A
where_to_add.number += 1

B.add_1()
print(A.number)
B.add_1()
print(A.number)
B.add_1()
print(A.number)





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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    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%2f53336487%2fget-object-on-which-a-method-was-called-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If you never plan on reassigning math_ops outside A, this is fairly simple to do.



    from modules import math_ops

    class A():
    def __init__():
    self.math_ops = math_ops.B(self)
    self.number = 1


    modules/math_ops.py:



    class B():
    def __init__(self, creator):
    self.creator = creator

    def add_1():
    creator.number += 1


    I will mention it again in case you skimmed the first line, the following will generate unexpected results since B is tracking the creator of the object rather than the caller.



    a1 = A()
    a2 = A()
    a1.math_ops = a2.math_ops
    a1.math_ops.add_1() # a2 is updated


    If that looks like something you might wanna do, the answer is a tad more complicated. Here's my attempt:



    from modules import math_ops
    class A():
    def __init__(self):
    self._math_ops = math_ops.B(self)
    self.number = 1

    @property
    def math_ops(self):
    self._math_ops.set_caller(self)
    return self._math_ops

    @math_ops.setter
    def math_ops(self, new_math_ops):
    self._math_ops = new_math_ops


    modules/math_ops.py:



    class B():
    def __init__(self, caller):
    self.caller = caller

    def set_caller(self, caller):
    self.caller = caller

    def add_1(self):
    self.caller.number += 1





    share|improve this answer
























    • Looks good, but one question: Wouldn't it create a circular link and therefore also a possibility for memory leaks to keep the reference to the A object in B as we are also keeping a reference to B from A?

      – pesekon2
      Nov 16 '18 at 13:52











    • @pesekon2 Not if they don't have delete methods. See this answer.

      – merlyn
      Nov 16 '18 at 14:01











    • Oh, I see. This is for sure the correct answer, just one more question before accepting it: The memory is not released immediately and then when I am creating in a loop a lot of big objects reqriting each other (e.g. in each loop is a1 = A()), I can run out of memory when not using gc.collect(). Is there another way to avoid it besides including it in the property definition?

      – pesekon2
      Nov 19 '18 at 14:37











    • @pesekon2 You don't have to use gc.collect(). The gc in python is enabled by default. It will collect unclaimed objects automatically before you run out of memory.

      – merlyn
      Nov 19 '18 at 15:05
















    1














    If you never plan on reassigning math_ops outside A, this is fairly simple to do.



    from modules import math_ops

    class A():
    def __init__():
    self.math_ops = math_ops.B(self)
    self.number = 1


    modules/math_ops.py:



    class B():
    def __init__(self, creator):
    self.creator = creator

    def add_1():
    creator.number += 1


    I will mention it again in case you skimmed the first line, the following will generate unexpected results since B is tracking the creator of the object rather than the caller.



    a1 = A()
    a2 = A()
    a1.math_ops = a2.math_ops
    a1.math_ops.add_1() # a2 is updated


    If that looks like something you might wanna do, the answer is a tad more complicated. Here's my attempt:



    from modules import math_ops
    class A():
    def __init__(self):
    self._math_ops = math_ops.B(self)
    self.number = 1

    @property
    def math_ops(self):
    self._math_ops.set_caller(self)
    return self._math_ops

    @math_ops.setter
    def math_ops(self, new_math_ops):
    self._math_ops = new_math_ops


    modules/math_ops.py:



    class B():
    def __init__(self, caller):
    self.caller = caller

    def set_caller(self, caller):
    self.caller = caller

    def add_1(self):
    self.caller.number += 1





    share|improve this answer
























    • Looks good, but one question: Wouldn't it create a circular link and therefore also a possibility for memory leaks to keep the reference to the A object in B as we are also keeping a reference to B from A?

      – pesekon2
      Nov 16 '18 at 13:52











    • @pesekon2 Not if they don't have delete methods. See this answer.

      – merlyn
      Nov 16 '18 at 14:01











    • Oh, I see. This is for sure the correct answer, just one more question before accepting it: The memory is not released immediately and then when I am creating in a loop a lot of big objects reqriting each other (e.g. in each loop is a1 = A()), I can run out of memory when not using gc.collect(). Is there another way to avoid it besides including it in the property definition?

      – pesekon2
      Nov 19 '18 at 14:37











    • @pesekon2 You don't have to use gc.collect(). The gc in python is enabled by default. It will collect unclaimed objects automatically before you run out of memory.

      – merlyn
      Nov 19 '18 at 15:05














    1












    1








    1







    If you never plan on reassigning math_ops outside A, this is fairly simple to do.



    from modules import math_ops

    class A():
    def __init__():
    self.math_ops = math_ops.B(self)
    self.number = 1


    modules/math_ops.py:



    class B():
    def __init__(self, creator):
    self.creator = creator

    def add_1():
    creator.number += 1


    I will mention it again in case you skimmed the first line, the following will generate unexpected results since B is tracking the creator of the object rather than the caller.



    a1 = A()
    a2 = A()
    a1.math_ops = a2.math_ops
    a1.math_ops.add_1() # a2 is updated


    If that looks like something you might wanna do, the answer is a tad more complicated. Here's my attempt:



    from modules import math_ops
    class A():
    def __init__(self):
    self._math_ops = math_ops.B(self)
    self.number = 1

    @property
    def math_ops(self):
    self._math_ops.set_caller(self)
    return self._math_ops

    @math_ops.setter
    def math_ops(self, new_math_ops):
    self._math_ops = new_math_ops


    modules/math_ops.py:



    class B():
    def __init__(self, caller):
    self.caller = caller

    def set_caller(self, caller):
    self.caller = caller

    def add_1(self):
    self.caller.number += 1





    share|improve this answer













    If you never plan on reassigning math_ops outside A, this is fairly simple to do.



    from modules import math_ops

    class A():
    def __init__():
    self.math_ops = math_ops.B(self)
    self.number = 1


    modules/math_ops.py:



    class B():
    def __init__(self, creator):
    self.creator = creator

    def add_1():
    creator.number += 1


    I will mention it again in case you skimmed the first line, the following will generate unexpected results since B is tracking the creator of the object rather than the caller.



    a1 = A()
    a2 = A()
    a1.math_ops = a2.math_ops
    a1.math_ops.add_1() # a2 is updated


    If that looks like something you might wanna do, the answer is a tad more complicated. Here's my attempt:



    from modules import math_ops
    class A():
    def __init__(self):
    self._math_ops = math_ops.B(self)
    self.number = 1

    @property
    def math_ops(self):
    self._math_ops.set_caller(self)
    return self._math_ops

    @math_ops.setter
    def math_ops(self, new_math_ops):
    self._math_ops = new_math_ops


    modules/math_ops.py:



    class B():
    def __init__(self, caller):
    self.caller = caller

    def set_caller(self, caller):
    self.caller = caller

    def add_1(self):
    self.caller.number += 1






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 13:04









    merlynmerlyn

    1,77011323




    1,77011323













    • Looks good, but one question: Wouldn't it create a circular link and therefore also a possibility for memory leaks to keep the reference to the A object in B as we are also keeping a reference to B from A?

      – pesekon2
      Nov 16 '18 at 13:52











    • @pesekon2 Not if they don't have delete methods. See this answer.

      – merlyn
      Nov 16 '18 at 14:01











    • Oh, I see. This is for sure the correct answer, just one more question before accepting it: The memory is not released immediately and then when I am creating in a loop a lot of big objects reqriting each other (e.g. in each loop is a1 = A()), I can run out of memory when not using gc.collect(). Is there another way to avoid it besides including it in the property definition?

      – pesekon2
      Nov 19 '18 at 14:37











    • @pesekon2 You don't have to use gc.collect(). The gc in python is enabled by default. It will collect unclaimed objects automatically before you run out of memory.

      – merlyn
      Nov 19 '18 at 15:05



















    • Looks good, but one question: Wouldn't it create a circular link and therefore also a possibility for memory leaks to keep the reference to the A object in B as we are also keeping a reference to B from A?

      – pesekon2
      Nov 16 '18 at 13:52











    • @pesekon2 Not if they don't have delete methods. See this answer.

      – merlyn
      Nov 16 '18 at 14:01











    • Oh, I see. This is for sure the correct answer, just one more question before accepting it: The memory is not released immediately and then when I am creating in a loop a lot of big objects reqriting each other (e.g. in each loop is a1 = A()), I can run out of memory when not using gc.collect(). Is there another way to avoid it besides including it in the property definition?

      – pesekon2
      Nov 19 '18 at 14:37











    • @pesekon2 You don't have to use gc.collect(). The gc in python is enabled by default. It will collect unclaimed objects automatically before you run out of memory.

      – merlyn
      Nov 19 '18 at 15:05

















    Looks good, but one question: Wouldn't it create a circular link and therefore also a possibility for memory leaks to keep the reference to the A object in B as we are also keeping a reference to B from A?

    – pesekon2
    Nov 16 '18 at 13:52





    Looks good, but one question: Wouldn't it create a circular link and therefore also a possibility for memory leaks to keep the reference to the A object in B as we are also keeping a reference to B from A?

    – pesekon2
    Nov 16 '18 at 13:52













    @pesekon2 Not if they don't have delete methods. See this answer.

    – merlyn
    Nov 16 '18 at 14:01





    @pesekon2 Not if they don't have delete methods. See this answer.

    – merlyn
    Nov 16 '18 at 14:01













    Oh, I see. This is for sure the correct answer, just one more question before accepting it: The memory is not released immediately and then when I am creating in a loop a lot of big objects reqriting each other (e.g. in each loop is a1 = A()), I can run out of memory when not using gc.collect(). Is there another way to avoid it besides including it in the property definition?

    – pesekon2
    Nov 19 '18 at 14:37





    Oh, I see. This is for sure the correct answer, just one more question before accepting it: The memory is not released immediately and then when I am creating in a loop a lot of big objects reqriting each other (e.g. in each loop is a1 = A()), I can run out of memory when not using gc.collect(). Is there another way to avoid it besides including it in the property definition?

    – pesekon2
    Nov 19 '18 at 14:37













    @pesekon2 You don't have to use gc.collect(). The gc in python is enabled by default. It will collect unclaimed objects automatically before you run out of memory.

    – merlyn
    Nov 19 '18 at 15:05





    @pesekon2 You don't have to use gc.collect(). The gc in python is enabled by default. It will collect unclaimed objects automatically before you run out of memory.

    – merlyn
    Nov 19 '18 at 15:05













    -1














    class A():
    number = 1

    class B():
    def add_1():
    where_to_add = A
    where_to_add.number += 1

    B.add_1()
    print(A.number)
    B.add_1()
    print(A.number)
    B.add_1()
    print(A.number)





    share|improve this answer




























      -1














      class A():
      number = 1

      class B():
      def add_1():
      where_to_add = A
      where_to_add.number += 1

      B.add_1()
      print(A.number)
      B.add_1()
      print(A.number)
      B.add_1()
      print(A.number)





      share|improve this answer


























        -1












        -1








        -1







        class A():
        number = 1

        class B():
        def add_1():
        where_to_add = A
        where_to_add.number += 1

        B.add_1()
        print(A.number)
        B.add_1()
        print(A.number)
        B.add_1()
        print(A.number)





        share|improve this answer













        class A():
        number = 1

        class B():
        def add_1():
        where_to_add = A
        where_to_add.number += 1

        B.add_1()
        print(A.number)
        B.add_1()
        print(A.number)
        B.add_1()
        print(A.number)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 11:07









        KarlKarl

        2,50943055




        2,50943055






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336487%2fget-object-on-which-a-method-was-called-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly