Special Method Protocols

__get__() — attributes protocol

Implement descriptor reads. When accessed on a class, instance is None; on an instance it is the receiving object. A descriptor with __get__ alone is non-data and can be shadowed by instance attributes.

Formal Syntax & Structure

python syntaxpython
def __get__(self, instance, owner=None):

Parameters & Attributes

NameTypeDescription
instanceobject | NoneInstance or None for class access.
ownertype | NoneOwning class.

Return Value

object

Descriptor result.

Working Example

Example Snippetpython
class Field:    def __get__(self, instance, owner=None):        if instance is None: return self        return 7class Record:    count = Field()print(Record().count)
Python Reference