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)
#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


Messages In This Thread
RE: Difference between self and a reference to own class. (unique instance) - by Gribouillis - Nov-01-2018, 11:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  can Inner Class reference the Outer Class's static variable? raykuan 6 6,066 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Access instance of a class Pavel_47 5 2,163 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,991 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  Class Instance angus1964 4 2,508 Jun-22-2021, 08:50 AM
Last Post: angus1964
  Can we access instance variable of parent class in child class using inheritance akdube 3 14,056 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,601 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Issue referencing new instance from other class nanok66 3 2,272 Jul-31-2020, 02:07 AM
Last Post: nanok66
  Class variable / instance variable ifigazsi 9 4,428 Jul-28-2020, 11:40 AM
Last Post: buran
  Python complains that class instance is not defined colt 3 5,747 Sep-17-2019, 12:32 AM
Last Post: ichabod801
  how to add class instance attributes from list 999masks 2 2,771 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