Python – the difference between proxy class and delegation in Python

classclass-designpython

Wiki:

A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes

Hmm!, what would this look like in terms of code ?

Wiki

delegation refers to one object relying upon another to provide a specified set of functionalities

This class for example delegates its functionalities to another object:

class CustomInt: 

    def __init__(self): 
        self.obj = int() 

    def __getattr__(self, attr): 
        return getattr(self.obj, attr)   # Delegation

Since it's a class functioning as an interface for something else, can I consider it as a proxy class ?

Best Answer

A proxy has no other functionality than to forward actions. A class that delegates also adds its own functionality.
In that, effectively, a proxy is a special case of delegation.
As @Jules said, your example is one of a proxy.

Related Topic