Sobes.tech
Junior — Middle

Can you give examples of different ways to reference objects in programming?

sobes.tech AI

Answer from AI

In programming, there are different ways to refer to objects:

  • Direct references (pointers) — variables that store the memory address of an object (for example, in C/C++).
  • References — alternative names for objects, often used in languages like C++ or Java.
  • Object identifiers — unique keys or IDs that allow retrieving an object from a collection or database.
  • Weak references — references that do not prevent garbage collection, used for caching.

Example in Python using a weak reference:

import weakref

class MyObject:
    pass

obj = MyObject()
weak_obj = weakref.ref(obj)

print(weak_obj())  # Will output the object

del obj
print(weak_obj())  # Now None, as the object has been deleted