Python Forum
Difference between self and a reference to own class. (unique instance)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference between self and a reference to own class. (unique instance)
#1
class external(threading.Thread):
    i = 0
    def __init__(self, dir_q, result_q):
        super(external, self).__init__()
then right after "super", I write:
    self.i = 5
--external.i returns 0 (from another object)
    external.i = 5
--external.i returns 5 (from another object)

What is the difference, if only one instance of external exists?

By the way, self.i returns 5 if executed inside of a method from this instance in either way. If the instances sees self.i equal to external.i, why do other classes see external.i different that what this sees as self.i?
Reply
#2
The class has its own dictionary, external.__dict__. This is where external.i is stored.

Each instance of the class also has its own dictionary self.__dict__. This is where self.i is stored.

When one writes external.i, python searches the member i in the class' dictionary and if it does not find one, it searches in the ancestor classes. When one writes self.i, python searches in the instance's dictionary and if it does not find one, it searches in the class' dictionary, then in the ancestor classes.

If you have a C++ background, you might expect a different behavior for class members. I once wrote a function to simulate a C++ static field in a python class
def static_cpp():
    from functools import partial
    _default = object()
    
    def _helper_static_cpp(container, value = _default):
        if value is not _default:
            container[0] = value
        return container[0]
            
    def static_cpp(initializer = None):
        """Declarator to imitate a c++ static field in a class (with accessor).
        
                >>> class A(object):
                ...     foo = static_cpp(1.24)
                ...
                >>> print( A.foo() )
                1.24
                >>> A.foo(555)
                >>> print( A.foo() )
                555
                >>> a = A()
                >>> print( a.foo() )
                555
                >>> a.foo(666)
                >>> print( A.foo() )
                666
        """
        return partial(_helper_static_cpp, [initializer])
    return static_cpp

static_cpp = static_cpp()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,882 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Access instance of a class Pavel_47 5 2,083 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,861 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  Class Instance angus1964 4 2,440 Jun-22-2021, 08:50 AM
Last Post: angus1964
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,976 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,551 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Issue referencing new instance from other class nanok66 3 2,215 Jul-31-2020, 02:07 AM
Last Post: nanok66
  Class variable / instance variable ifigazsi 9 4,301 Jul-28-2020, 11:40 AM
Last Post: buran
  Python complains that class instance is not defined colt 3 5,635 Sep-17-2019, 12:32 AM
Last Post: ichabod801
  how to add class instance attributes from list 999masks 2 2,703 Jul-22-2019, 07:59 AM
Last Post: 999masks

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020